package com.hongyegroup.cpt_parttime.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import com.guadou.cs_cptservices.base.activity.YYBaseVDBLoadingActivity
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.ext.click
import com.guadou.lib_baselib.ext.vertical
import com.guadou.lib_baselib.view.gloading.GLoadingTitleStatus
import com.guadou.lib_baselib.view.gloading.Gloading
import com.guadou.lib_baselib.view.gloading.GloadingPlaceHolderlAdapter
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.databinding.ActivityParttimeIndustryBinding
import com.hongyegroup.cpt_parttime.mvvm.PartTimeIndustryViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PartTimeIndustryActivity : YYBaseVDBLoadingActivity<PartTimeIndustryViewModel, ActivityParttimeIndustryBinding>(), OnRefreshListener {

    var selectedIds: ArrayList<String>? = null

    //重新生成GLoading对象
    override fun generateGLoading(): Gloading.Holder {
        return Gloading.from(GloadingPlaceHolderlAdapter(R.layout.layout_placeholder_grid))
            .wrap(this, GLoadingTitleStatus(true, true, false))
            .withRetry { onGoadingRetry() }
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.activity_parttime_industry)
    }

    override fun getDataFromIntent(intent: Intent) {
        selectedIds = intent.getStringArrayListExtra("industryIds")
    }

    override fun init(savedInstanceState: Bundle?) {
        initRV()
        initData()
        initListener()
    }

    private fun initData() {
        mViewModel.getIndustryList(selectedIds).observe(this) {
            mBinding.refreshLayout.finishRefresh()
        }
    }

    private fun initRV() {
        mBinding.recyclerView.vertical(2).adapter = mViewModel.mAdapter
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initListener() {
        mViewModel.mAdapter.loadMoreModule.isEnableLoadMore = false

        //刷新控件初始化
        mBinding.refreshLayout.setEnableNestedScroll(true)
        mBinding.refreshLayout.setEnableLoadMore(false)
        mBinding.refreshLayout.setEnableRefresh(true)
        mBinding.refreshLayout.setOnRefreshListener(this)

        //点击事件
        mViewModel.mAdapter.setOnItemClickListener { _, view, position ->
            mViewModel.mDatas[position].apply {
                isSelected = !isSelected
            }
            mViewModel.mAdapter.notifyItemChanged(position, "check")
        }

        mBinding.easyTitle.addRightText("重置") {
            mViewModel.mDatas.forEach {
                it.isSelected = false
            }
            mViewModel.mAdapter.notifyDataSetChanged()
        }

        //点击提交
        mBinding.buttonApply.click {
            val list = mViewModel.mDatas.filter { it.isSelected }.map { it.industry_id }
            setResult(RESULT_OK, Intent().apply {
                putStringArrayListExtra("industryIds", ArrayList(list))
            })
            finish()
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        initData()
    }

    override fun startObserve() {

    }

}