package com.hongyegroup.cpt_parttime.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.guadou.cs_cptservices.base.activity.YYBaseVDBActivity
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.extLoad
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.ext.bindFragment
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.utils.StatusBarUtils
import com.hongyegroup.cpt_parttime.BR
import com.hongyegroup.cpt_parttime.R
import com.hongyegroup.cpt_parttime.databinding.ActivityParttimeJobDetailBinding
import com.hongyegroup.cpt_parttime.mvvm.PartTimeJobDetailViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PartTimeJobDetailActivity : YYBaseVDBActivity<PartTimeJobDetailViewModel, ActivityParttimeJobDetailBinding>() {

    private val mClickProxy by lazy { ClickProxy() }
    private val titleList = mutableListOf<String>()  //TabLayot的Title
    private var mCurPagePosition = 0
    private val isFragmentShowedStatus = true

    private var mPartJobDetailFragment: PartJobDetailFragment? = null
    private var mPartJobDetailSlotsFragment: PartJobDetailSlotsFragment? = null
    private var mPartJobDetailProcessFragment: PartJobDetailProcessFragment? = null
    private var mPartJobDetailGroomingFragment: PartJobDetailGroomingFragment? = null
    private var mPartJobDetailPaymentFragment: PartJobDetailPaymentFragment? = null
    private var mPartJobDetailFaqFragment: PartJobDetailFaqFragment? = null

    companion object {
        fun startInstance() {
            val context = CommUtils.getContext()
            val intent = Intent(context, PartTimeJobDetailActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.activity_parttime_job_detail, BR.viewModel, mViewModel)
            .addBindingParams(BR.click, mClickProxy)
    }

    override fun init(savedInstanceState: Bundle?) {
        StatusBarUtils.immersive(this)
        setStatusBarWhiteText()

        initData()
    }

    //请求工作详情数据
    private fun initData() {
        //normal status
        titleList.add("工作详情")
        titleList.add("更多工作")
        for (i in titleList.indices) {
            val tab: TabLayout.Tab = mBinding.tabLayout.newTab()
            val view = CommUtils.inflate(R.layout.tab_job_detail_category)
            tab.customView = view
            val text = view.findViewById<TextView>(R.id.tab_text)
            text.text = titleList[i]
            mBinding.tabLayout.addTab(tab)
        }

        mBinding.ivJobImage.extLoad("https://img.zcool.cn/community/0160855ac4e9c6a801212573507ad7.png@1280w_1l_2o_100sh.png")

        mViewModel.requestJobDetail().observe(this) {
            initPager(hasHow2Report = true, hasGrooming = true)
        }
    }

    //初始化Tab和ViewPager
    private fun initPager(hasHow2Report: Boolean, hasGrooming: Boolean) {
        titleList.clear()
        mViewModel.mFragments.clear()

        mPartJobDetailFragment = PartJobDetailFragment()
        mViewModel.mFragments.add(mPartJobDetailFragment!!)
        titleList.add("工作详情")

        mPartJobDetailSlotsFragment = PartJobDetailSlotsFragment()
        mViewModel.mFragments.add(mPartJobDetailSlotsFragment!!)
        titleList.add("更多工作")

        if (hasHow2Report) {
            mPartJobDetailProcessFragment = PartJobDetailProcessFragment()
            mViewModel.mFragments.add(mPartJobDetailProcessFragment!!)
            titleList.add("工作流程")
        }

        if (hasGrooming) {
            mPartJobDetailGroomingFragment = PartJobDetailGroomingFragment()
            mViewModel.mFragments.add(mPartJobDetailGroomingFragment!!)
            titleList.add("着装")
        }

        mPartJobDetailPaymentFragment = PartJobDetailPaymentFragment()
        mViewModel.mFragments.add(mPartJobDetailPaymentFragment!!)
        titleList.add("付款流程")

        mPartJobDetailFaqFragment = PartJobDetailFaqFragment()
        mViewModel.mFragments.add(mPartJobDetailFaqFragment!!)
        titleList.add("常见问题")

        //设置Viewpager-Adapter
        mBinding.viewPager.bindFragment(supportFragmentManager, mViewModel.mFragments)

        //自定义Tab View,自定义下划线
        mBinding.tabLayout.removeAllTabs()
        for (i in titleList.indices) {
            val tab: TabLayout.Tab = mBinding.tabLayout.newTab()
            val view = CommUtils.inflate(R.layout.tab_job_detail_category)
            tab.customView = view
            val text = view.findViewById<TextView>(R.id.tab_text)
            text.text = titleList[i]
            mBinding.tabLayout.addTab(tab)
        }

        mBinding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {}
            override fun onPageScrollStateChanged(i: Int) {}
            override fun onPageSelected(i: Int) {
                mCurPagePosition = i
                mBinding.tabLayout.setScrollPosition(i, 0f, true)
                if (i == 0) {
                    if (isFragmentShowedStatus) {
                        mClickProxy.isShowBottomBtn.value = true
                    }
//                    if (!CheckUtil.isEmpty(mCurrentJobDetail.getJob().work_status_describe)) {
//                        mTvCancelStatusMessage.setVisibility(View.VISIBLE)
//                    }
                } else {
                    mClickProxy.isShowBottomBtn.value = false
                }

                mBinding.ivTabRight.visibility = if (i == titleList.size - 1) View.GONE else View.VISIBLE

                //监听滚动，设置是否禁用截屏功能
                setScreenshotEnable(i != 2)
            }
        })

        mBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                val position = tab.position
                mBinding.viewPager.setCurrentItem(position, true)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })
    }

    //是否禁止截屏
    private fun setScreenshotEnable(enable: Boolean) {
        if (enable) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
        }
    }

    override fun startObserve() {

    }


    /**
     * DataBinding事件处理
     */
    inner class ClickProxy {

        var isShowBottomBtn = MutableLiveData(true)

        //点击申请工作
        fun applyJob() {
            toast("点击申请工作")
        }

        //点击分享工作
        fun shareJob() {
            toast("点击分享工作")
        }

        //点击Tab下一个
        fun tabNext() {
            mBinding.viewPager.setCurrentItem(mCurPagePosition + 1, true)
        }

        //去相册页面
        fun gotoGalleryPage() {
            PartJobAlbumActivity.startInstance()
        }

    }
}