package com.example.chemical.ui.discard

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.chemical.ChemicalApp
import com.example.chemical.R
import com.example.chemical.databinding.ActivityLedgerBinding
import com.example.chemical.ui.SplashActivity
import com.example.chemical.ui.common.BaseCountDownActivity
import com.example.chemical.ui.fragments.InventoryIedgerFragment

import com.example.chemical.ui.fragments.UseLedgerFragment
import com.example.chemical.utils.TimeUpdater
import com.example.chemical.utils.UiManager
import com.example.chemical.weidith.SwipeViewPager
import com.example.chemical.weidith.TabFragmentPagerAdapter
import com.rc.httpcore.HttpClient
import com.rc.httpcore.HttpConfig

//电子台账
class LedgerActivity : BaseCountDownActivity<ActivityLedgerBinding>() {


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

    override fun createViewBinding() = ActivityLedgerBinding.inflate(LayoutInflater.from(this))

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)
        initTitle()
        fragmentInView()
        viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
        viewBinding.tvOutLogin.setOnClickListener {
            callLogoutApi {
                ChemicalApp.userData = null
                HttpClient.token = null
                ChemicalApp.subjectId = null
                UiManager.switcherCashier(this, SplashActivity::class.java)
            }
        }

        //返回
        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
        }
    }

    override fun initData() {
        super.initData()
        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 onBackPressed() {
        super.onBackPressed()
        finish()
    }

    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)
    }

}