package com.qimai.yttgrass

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.qimai.android.fetch.Response.Status
import com.qimai.yttgrass.adapter.ImageAdapter
import com.qimai.yttgrass.model.goodsInfoBean
import com.qimai.yttgrass.vm.HomeVm
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import kotlinx.android.synthetic.main.fragment_welcome.*

class WelcomeFragment(contentLayoutId: Int = R.layout.fragment_welcome) :
    Fragment(contentLayoutId) {
    private val mVm: HomeVm by lazy {
        ViewModelProvider(requireActivity()).get(HomeVm::class.java)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        addObserver()
        mVm.getBannerUrl().observe(requireActivity(), Observer {
            when (it.status) {
                Status.SUCCESS -> {
                    mVm.mBannerList.value = it.data?.data?.images
                }
                Status.ERROR -> {
                    Log.d("TAG", "onActivityCreated: error= ${it.message}")
                }
            }
        })
        mVm.getBannerUrl().observe(requireActivity(), Observer {
            when (it.status) {
                Status.SUCCESS -> {
                    mVm.mBannerList.value = it.data?.data?.images
                }
            }
        })
    }


    private fun updateBanner(datas: ArrayList<String>) {
        banner.addBannerLifecycleObserver(this)
            .setLoopTime(10000)
            .adapter = ImageAdapter(datas).apply {

        }
    }

    private fun addObserver() {
        mVm.mBannerList.observe(viewLifecycleOwner, Observer {
            updateBanner(it)
        })
        mVm.mCodeMutableList.observe(viewLifecycleOwner, Observer<String> {
            if (!it.isNullOrEmpty()) {
                // Toast.makeText(requireActivity(), it, Toast.LENGTH_LONG).show()
                //  Log.d("value", "addObserver: value = $it")
                val newValue = it
                mVm.mCodeMutableList.value = ""
                mVm.getGoodsInfo(newValue, "0", "44764")
                    .observe(requireActivity(), Observer {
                        when (it.status) {
                            Status.SUCCESS -> {
                                val goods = goodsInfoBean(
                                    it.data?.data?.goods?.images,
                                    it.data?.data?.name,
                                    it.data?.data?.goods?.content?.subheading,
                                    it.data?.data?.goods?.content?.content,
                                    it.data?.data?.goods_id
                                )
                                mVm.mGoodsInfo = goods
                                Navigation.findNavController(banner)
                                    .navigate(R.id.action_welcomeFragment_to_detailFragment)
                            }
                            Status.ERROR -> {
                                Toast.makeText(requireActivity(), it.message, Toast.LENGTH_LONG)
                                    .show()
                            }
                        }
                    })
            }
        })
    }


}

