package com.zj365.dc.activity.shop

import android.text.TextUtils
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.base.act.BaseListAct
import com.xty.base.act.BaseVmAct
import com.xty.common.Const
import com.xty.common.R
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.ShopChildCategoryAdapter
import com.zj365.dc.adapter.shop.ShopGoodsTwoAdapter
import com.zj365.dc.databinding.ActShopCategoryBinding
import com.zj365.dc.vm.ShopMallVm
import okhttp3.internal.notify
import java.util.ArrayList

@Route(path = ARouterUrl.SHOP_CATEGORY)
class ShopCategoryActivity : BaseVmAct<ShopMallVm>() {

    val binding by lazy { ActShopCategoryBinding.inflate(layoutInflater) }

    val mCategoryAdapter by lazy { ShopCategoryAdapter() }

    val mGoodsAdapter by lazy { ShopGoodsTwoAdapter() }

    val mChildCategoryAdapter by lazy { ShopChildCategoryAdapter() }

    var goodsName:String? = null

    var categoryId:Long = 0L
    var mCategoryList = ArrayList<ShopCategoryBean>()
    var parentId:Long = 0L

    var index = 0
    var page:Int = 1

     fun initAdapter() {



        binding.recyclerView.adapter = mGoodsAdapter
        binding.recyclerView.layoutManager = LinearLayoutManager(this@ShopCategoryActivity)

        binding.childRecyclerView.adapter = mChildCategoryAdapter
        binding.childRecyclerView.layoutManager = LinearLayoutManager(this@ShopCategoryActivity)

        mChildCategoryAdapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as ShopCategoryBean
            (adapter.data as ArrayList<ShopCategoryBean>).forEach {
                it.isChecked = false

            }
            bean.isChecked = true
            categoryId = bean.id
            mViewModel.getGoodsList(goodsName,categoryId,0,page)
            mChildCategoryAdapter.notifyDataSetChanged()
        }

         binding.mRefresh.setOnRefreshListener {
             page = 1
             loadData()
             binding.mRefresh.finishRefresh(1000)
         }
         binding.mRefresh.setEnableLoadMore(true)
         binding.mRefresh.setOnLoadMoreListener {
             page++
             loadData()
             binding.mRefresh.finishLoadMore(1000)
         }

         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}"
             )
             bundle.putBoolean("isShowTitle",false)
             RouteManager.goAct(ARouterUrl.H5_ACT, bundle)
         }

    }

    fun loadData(){
        mViewModel.getGoodsList(goodsName,categoryId,0,page)

    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text ="商品分类"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        categoryId = intent.getLongExtra("id",0)
        parentId = categoryId

        mCategoryList = intent.getParcelableArrayListExtra<ShopCategoryBean>("categoryList")!!
        initAdapter()
        mCategoryList.forEachIndexed { index, shopCategoryBean ->
            if (shopCategoryBean.id == categoryId){
                this.index = index
                shopCategoryBean.isChecked = true
            }
        }

        binding.categoryRecyclerView.adapter = mCategoryAdapter

        if(mCategoryList.size != 8){
            binding.categoryRecyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        }else if(mCategoryList.size == 8){
            binding.categoryRecyclerView.layoutManager = GridLayoutManager(this,4)
        }
        mCategoryAdapter.setNewInstance(mCategoryList)
        binding.categoryRecyclerView.scrollToPosition(index)

        mCategoryAdapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as ShopCategoryBean
            (adapter.data as ArrayList<ShopCategoryBean>).forEach {
                it.isChecked = false

            }
            bean.isChecked = true
            parentId = bean.id
            mViewModel.getGoodsCategory(bean.id)
            mCategoryAdapter.notifyDataSetChanged()
        }
        mViewModel.getGoodsCategory(categoryId)

        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,categoryId,0,page)
                    //mViewModel.getGoodsList(goodsName,0L,0,page)

                    RxKeyboardTool.hideSoftInput(this@ShopCategoryActivity)
                    return true

                }
                return false
            }

        })

    }


    override fun liveObserver() {
        mViewModel.categoryLiveDate.observe(this){
            it.data.add(0,ShopCategoryBean(parentId,parentId,"全部",0,null,true))
            it.data.forEachIndexed { index, shopCategoryBean ->

                if (shopCategoryBean.isChecked){
                    categoryId = shopCategoryBean.id
                    mViewModel.getGoodsList(goodsName,shopCategoryBean.id,0,page)

                }
            }
            mChildCategoryAdapter.setNewInstance(it.data)
        }


        mViewModel.goodsLiveData.observe(this){

            if (page == 1) {
                if (it.data.records.isNullOrEmpty()) {
                    mGoodsAdapter.setNewInstance(null)
                    mGoodsAdapter.setEmptyView(getNoneView())
                    binding.mRefresh?.setEnableLoadMore(false)
                } else {
                    if (it.data.records.size < Const.PAGE_SIZE) {
                        binding.mRefresh?.setEnableLoadMore(false)
                    } else {
                        binding.mRefresh?.setEnableLoadMore(true)
                        ++page
                    }
                    mGoodsAdapter.setNewInstance(it.data.records)
                }
            } else {
                if (it.data.records.isNullOrEmpty()) {
                    binding.mRefresh?.setEnableLoadMore(false)
                }else{
                    if (it.data.records.size < Const.PAGE_SIZE) {
                        binding.mRefresh?.setEnableLoadMore(false)
                    }else{
                        ++page
                    }
                    mGoodsAdapter.addData(it.data.records)

                }

            }
        }

    }

    fun getNoneView() =
        LayoutInflater.from(this).inflate(R.layout.view_none_data, binding.recyclerView, false)

    override fun setLayout() = binding.root
}