package com.example.chemical.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.chemical.ChemicalApp
import com.example.chemical.R
import com.example.chemical.databinding.FragmentQueryTwoBinding
import com.example.chemical.ui.adapter.UesItemDetail
import com.rc.core.log.RcLog
import com.rc.core.ui.fragment.RcBaseFragment
import com.rc.httpcore.bean.HxpStockWaitListBean
import com.rc.httpcore.bean.QueryTwoBean
import com.rc.httpcore.client.ApiRepository

//查询-详情列表
class QueryTwoFragment : RcBaseFragment<FragmentQueryTwoBinding>() {

    private val mAdapterUse by lazy { UesItemDetail() }  //化学品信息

    private var mPages = 1  //总页码
    private var mDataPage = 10  //返回得总条数
    private var page = 1
    private var pageSize = 10
    private var mStockId: String? = null
    private var mChemicalId: String? = null  //化学品id

    private var mCabinetId: String? = null
    private var mDoorId: String? = null

    companion object {
        fun newInstance(): QueryTwoFragment {
            return QueryTwoFragment()
        }
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ): FragmentQueryTwoBinding {
        return FragmentQueryTwoBinding.inflate(inflater, container, false)
    }

    override fun initViews(savedInstanceState: Bundle?) {
        viewBinding.rvUse?.layoutManager = LinearLayoutManager(requireActivity())
        viewBinding.rvUse?.adapter = mAdapterUse
        viewBinding.butUpper.setOnClickListener {
            //上一页
            if (mDataPage != 0) {
                if (page != 1) {
                    page--
                    getList()
                }
            }
        }

        viewBinding.butNext.setOnClickListener {
            //下一页
            if (mDataPage != 0) {
                if (mDataPage != page) {
                    page++
                    getList()
                }
            }
        }
    }

    private fun getList() {
        showLoading("查询中...")
        val mutableMap = mutableMapOf<String, String>()
//        mutableMap["stockId"] = mStockId!!
        mutableMap["chemicalId"] = mChemicalId!!
        mutableMap["subId"] = ChemicalApp.subjectId!!
        if (mCabinetId != null) {
            mutableMap["cabinetId"] = "$mCabinetId"
        }
        if (mDoorId != null) {
            mutableMap["doorId"] = "$mDoorId"
        }
        mutableMap["page"] = "$page"
        mutableMap["pageSize"] = "$pageSize"
        val disposable = ApiRepository.indexDetailsList(mutableMap)
            .subscribe({ data ->
                dismissLoading()
                mDataPage = data.pages  //总页
                if (data.pages != 0) {
                    if (data.pages > 1 && data.current > 1) {
                        //当前需要更新页码
                        upPagesView(data.pages, data.current)
                        mAdapterUse.setList(data.records.toMutableList())
                    } else {
                        mAdapterUse.setNewInstance(data.records.toMutableList())
                        initPagesView(data.pages)
                    }
                }
            }, { throwable ->
                dismissLoading()
                showNetError(throwable)

            })
        addDisposable(disposable)
    }

    fun setData(
        stockId: String,
        hxpStockWaitListBean: HxpStockWaitListBean,
        cabinetId: String?,
        doorId: String?
    ) {
        val chemicalName = hxpStockWaitListBean.chemicalName
        val chemicalLevelName = hxpStockWaitListBean.chemicalLevelName
        val chemicalCategoryName = hxpStockWaitListBean.chemicalCategoryName
        var sep = "液体"
        val specUnit = hxpStockWaitListBean.specUnit  //ml  / g
        if (specUnit.equals("g")) {
            sep = "固体"
        }
        val casNum = hxpStockWaitListBean.casNum //CAS
        val chemicalPurity = hxpStockWaitListBean.chemicalPurity //纯度
        val surplus = hxpStockWaitListBean.surplus //总量
        val chemicalNumber = hxpStockWaitListBean.chemicalNumber //数量
        viewBinding.chemicalName.text = "$chemicalName"
        viewBinding.chemicalLevelName.text = "$chemicalLevelName"
        viewBinding.chemicalCategoryName.text = "$chemicalCategoryName"

        if ((chemicalPurity != null && chemicalPurity.isNotBlank()) && (casNum != null && casNum.isNotBlank())) {
            viewBinding.cont.text = "$sep/${casNum}/${chemicalPurity}"
        } else if (casNum != null && casNum.isNotBlank()) {
            viewBinding.cont.text = "$sep/${casNum}"
        } else if (chemicalPurity != null && chemicalPurity.isNotBlank()) {
            viewBinding.cont.text = "$sep/${chemicalPurity}"
        } else {
            viewBinding.cont.text = "$sep"
        }

        viewBinding.surplus.text = "总量:$surplus$specUnit"
        viewBinding.chemicalNumber.text = "数量:$chemicalNumber"
//        mStockId = stockId
        mChemicalId = hxpStockWaitListBean.chemicalId
        mCabinetId = cabinetId
        mDoorId = doorId
        page=1
        pageSize=10
        getList()
    }

    /**
     * 总页码 当前页  初始化
     */
    private fun initPagesView(pages: Int) {
        mPages = pages
        viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_page)
        viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
        viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
        viewBinding.one.setTextColor(ContextCompat.getColor(requireActivity(), R.color.white))
        viewBinding.two.setTextColor(ContextCompat.getColor(requireActivity(), R.color.black))

        when (pages) {
            1 -> {
                viewBinding.one.visibility = View.VISIBLE
            }
            2 -> {
                viewBinding.two.visibility = View.VISIBLE
            }
            3 -> {
                viewBinding.two.visibility = View.VISIBLE
                viewBinding.three.visibility = View.VISIBLE
            }
            else -> {
                viewBinding.two.visibility = View.VISIBLE
                viewBinding.three.visibility = View.VISIBLE
                viewBinding.drop.visibility = View.VISIBLE
                viewBinding.many.visibility = View.VISIBLE
                viewBinding.many.text = "$pages"
            }
        }
        if (pages == 1 || pages == 0) {
            viewBinding.two.visibility = View.GONE
            viewBinding.three.visibility = View.GONE
            viewBinding.many.visibility = View.GONE
            viewBinding.drop.visibility = View.GONE
        }
        if (pages < 4) {
            when (pages) {
                1 -> {
                    viewBinding.two.visibility = View.GONE
                    viewBinding.three.visibility = View.GONE
                    viewBinding.many.visibility = View.GONE
                    viewBinding.drop.visibility = View.GONE
                }
                2 -> {
                    viewBinding.three.visibility = View.GONE
                    viewBinding.many.visibility = View.GONE
                    viewBinding.drop.visibility = View.GONE
                }
                3 -> {
                    viewBinding.many.visibility = View.GONE
                    viewBinding.drop.visibility = View.GONE
                }
            }
        }
    }

    //更新页码
    private fun upPagesView(pages: Int, current: Int) {
        RcLog.info("======2")
        if (pages < 4) {
//            viewBinding.one.visibility = View.VISIBLE
            when (pages) {
                1 -> {
                    viewBinding.two.visibility = View.GONE
                    viewBinding.three.visibility = View.GONE
                    viewBinding.many.visibility = View.GONE
                    viewBinding.drop.visibility = View.GONE
                }
                2 -> {
                    viewBinding.three.visibility = View.GONE
                    viewBinding.many.visibility = View.GONE
                    viewBinding.drop.visibility = View.GONE
                }
                3 -> {
                    viewBinding.many.visibility = View.GONE
                    viewBinding.drop.visibility = View.GONE
                }
            }
            when (current) {
                1 -> {
                    viewBinding.one.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.white
                        )
                    )
                    viewBinding.two.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.three.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_page)
                    viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                }
                2 -> {
//                    viewBinding.two.visibility = View.VISIBLE
                    viewBinding.one.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.two.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.white
                        )
                    )
                    viewBinding.three.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_page)
                    viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                }
                3 -> {
//                    viewBinding.three.visibility = View.VISIBLE
                    viewBinding.one.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.two.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.three.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.white
                        )
                    )
                    viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_page)
                }
            }
        } else {
//            viewBinding.many.visibility = View.VISIBLE
            viewBinding.three.text = "3"
            when (current) {
                1 -> {
                    viewBinding.one.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.white
                        )
                    )
                    viewBinding.two.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.three.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_page)
                    viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                }
                2 -> {
                    viewBinding.one.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.two.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.white
                        )
                    )
                    viewBinding.three.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_page)
                    viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                }
                3 -> {
                    viewBinding.one.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.two.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.black
                        )
                    )
                    viewBinding.three.setTextColor(
                        ContextCompat.getColor(
                            requireActivity(),
                            R.color.white
                        )
                    )
                    viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_page)
                }
                else -> {
                    viewBinding.three.text = "$current"
                    if (pages == current) {
                        viewBinding.three.text = "${current - 1}"

                        viewBinding.one.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.black
                            )
                        )
                        viewBinding.two.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.black
                            )
                        )
                        viewBinding.three.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.black
                            )
                        )
                        viewBinding.many.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.white
                            )
                        )
                        viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                        viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                        viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                        viewBinding.many.setBackgroundResource(R.drawable.bg_sou_suo_page)

                    } else {
                        viewBinding.one.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.black
                            )
                        )
                        viewBinding.two.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.black
                            )
                        )
                        viewBinding.three.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.white
                            )
                        )
                        viewBinding.many.setTextColor(
                            ContextCompat.getColor(
                                requireActivity(),
                                R.color.black
                            )
                        )

                        viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                        viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                        viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_page)
                        viewBinding.many.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
                    }
                }
            }
        }
    }

}