package xn.hxp.ui.discard

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import androidx.viewbinding.ViewBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.rc.core.ui.activity.BaseActivity
import xn.hxp.app.ChemicalApp
import xn.hxp.R
import xn.hxp.databinding.ActivityLedgerBinding
import xn.hxp.ui.fragments.InventoryIedgerFragment

import xn.hxp.ui.fragments.UseLedgerFragment
import xn.hxp.utils.TimeUpdater
import xn.hxp.utils.UiManager
import xn.hxp.weidith.SwipeViewPager
import xn.hxp.weidith.TabFragmentPagerAdapter
import com.rc.httpcore.HttpClient
import com.rc.httpcore.HttpConfig
import com.rc.httpcore.client.HttpTool

//电子台账
class LedgerActivity : BaseActivity() {


    private lateinit var timeUpdater: TimeUpdater
    private lateinit var viewPager: SwipeViewPager
    private lateinit var pagerAdapter: TabFragmentPagerAdapter
    lateinit var viewBinding: ActivityLedgerBinding

    override fun setViewBinding(): ViewBinding {
        viewBinding = ActivityLedgerBinding.inflate(layoutInflater)
        return viewBinding;
    }

    override fun onInit() {
        initTitle()
        fragmentInView()
        viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
        viewBinding.tvOutLogin.setOnClickListener {
            HttpTool.logout()
        }

        //返回
        viewBinding.tvReturn.setOnClickListener {
            finish()
        }

        //使用台账
        viewBinding.radioOne.setOnClickListener {

            viewBinding.radioOne.isChecked = true
            viewBinding.radioTwo.isChecked = false
            viewBinding.radioOne.setTextColor(ContextCompat.getColor(this, R.color.purple_ff54a2ea))
            viewBinding.radioTwo.setTextColor(ContextCompat.getColor(this, R.color.black))
            viewPager.currentItem = 0
        }

        //库存台账
        viewBinding.radioTwo.setOnClickListener {

            viewBinding.radioTwo.isChecked = true
            viewBinding.radioOne.isChecked = false
            viewBinding.radioOne.setTextColor(ContextCompat.getColor(this, R.color.black))
            viewBinding.radioTwo.setTextColor(ContextCompat.getColor(this, R.color.purple_ff54a2ea))
            viewPager.currentItem = 1
        }
        val handler = Handler(Looper.getMainLooper())
        timeUpdater = TimeUpdater(handler) { currentTime ->
            viewBinding.nowTime.text = "$currentTime"
        }
        // 启动定时更新
        timeUpdater.startUpdating()
    }


    override fun onDestroy() {
        super.onDestroy()
        try {
            timeUpdater.stopUpdating()
        } catch (e: Exception) {
        }
    }

    override fun cdFinish() {
        finish()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    override fun cdTime(cd: Int) {
        viewBinding.tvReturn.text = "返回${cd}s"
    }

    private fun fragmentInView() {
        val tabArray = mutableListOf(
            UseLedgerFragment.newInstance(),
            InventoryIedgerFragment.newInstance(),
        )
        pagerAdapter = TabFragmentPagerAdapter(supportFragmentManager, tabArray)
        viewPager = viewBinding.swipePagerView
        viewPager.adapter = pagerAdapter
    }


    private fun initTitle() {
        viewBinding.tvName.text = ChemicalApp.userData!!.userName
        val imageView = viewBinding.imageName
        // 使用 Glide 加载网络图片
        Glide.with(this)
            .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
            .into(imageView)

        // 使用 Glide 加载网络图片
        viewBinding.deptName.text = "${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"
        Glide.with(this)
            .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
            .into(viewBinding.image)
    }

}