package com.zj365.dc.fragment

import android.graphics.Rect
import android.os.Parcelable
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.base.fragment.BaseFragList
import com.xty.base.fragment.BaseVmFrag
import com.xty.base.vm.BaseVm
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.CommonToastUtils
import com.xty.network.model.GoodsRecordsBean
import com.xty.network.model.ShopCategoryBean
import com.zj365.dc.BuildConfig
import com.zj365.dc.adapter.shop.ShopCategoryAdapter
import com.zj365.dc.adapter.shop.ShopGoodsAdapter
import com.zj365.dc.databinding.FragShopBinding
import com.zj365.dc.vm.ShopMallVm
import java.util.ArrayList

class ShopFragment: BaseFragList<ShopMallVm>() {
    val binding by lazy { FragShopBinding.inflate(layoutInflater) }

    val mCategoryAdapter by lazy { ShopCategoryAdapter() }

    val mGoodsAdapter by lazy { ShopGoodsAdapter() }


    var goodsName:String? = null

    override fun initAdapter() {
        binding.recyclerView.adapter = mGoodsAdapter
        binding.recyclerView.layoutManager = GridLayoutManager(requireContext(),2)

        binding.categoryRecyclerView.adapter = mCategoryAdapter

        mCategoryAdapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as ShopCategoryBean
            bundle.clear()
            bundle.putLong("id",bean.id)
            bundle.putParcelableArrayList("categoryList",adapter.data as ArrayList<ShopCategoryBean>)
            RouteManager.goAct(ARouterUrl.SHOP_CATEGORY,bundle)
        }

        setRecycleRefresh(binding.recyclerView,binding.mRefresh,true)

        mGoodsAdapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as GoodsRecordsBean.GoodsBean
            bundle.clear()
            bundle.putString(
                "web_site_url",
                "${BuildConfig.h5url}pages/index/index?pageId=commodityDetail&id=${bean.id}"
            )
            RouteManager.goAct(ARouterUrl.H5_ACT, bundle)
        }
    }

    override fun refresh() {
        mViewModel.getGoodsList(null,0L,0,page)
    }

    override fun initView() {
        super.initView()
        mViewModel.getGoodsCategory(0L)
        mViewModel.getGoodsList(null,0L,0,page)

        binding.etSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
            override fun onEditorAction(p0: TextView?, p1: Int, p2: KeyEvent?): Boolean {
                if(p1 == EditorInfo.IME_ACTION_SEARCH){
                    goodsName = binding.etSearch.text.toString().trim()
                    if(TextUtils.isEmpty(goodsName)){
                        CommonToastUtils.showToast("请输入搜索内容")
                        return true
                    }
                    page = 1
                    mViewModel.getGoodsList(goodsName,0L,0,page)

                    RxKeyboardTool.hideSoftInput(requireActivity())
                    return true

                }
                return false
            }

        })

        binding.recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
                super.getItemOffsets(outRect, itemPosition, parent)
                outRect.bottom = 12
                outRect.left = 7
                outRect.right = 7
            }
        })
    }

    override fun setViewModel()= ShopMallVm()
    override fun observer() {
        mViewModel.categoryLiveDate.observe(this){
            if(it.data.size != 8){
                binding.categoryRecyclerView.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.HORIZONTAL,false)
            }else if(it.data.size == 8){
                binding.categoryRecyclerView.layoutManager = GridLayoutManager(requireContext(),4)

            }

            mCategoryAdapter.setNewInstance(it.data)
        }

        mViewModel.goodsLiveData.observe(this){
            if(page == 1){
                mGoodsAdapter.setNewInstance(it.data.records)

            }else{
               mGoodsAdapter.addData(it.data.records)
            }
        }
    }

    override fun setLayout() = binding.root
}