package com.dahe.gldriver.ui

import android.Manifest
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.MyHomePagerAdapter
import com.dahe.gldriver.bean.TabBean
import com.dahe.gldriver.databinding.ActivityHomeBinding
import com.dahe.gldriver.ui.home.HomeFragment
import com.dahe.gldriver.ui.message.MessageFragment
import com.dahe.gldriver.ui.mine.MineFragment
import com.dahe.gldriver.ui.waybill.WaybillFragment
import com.dahe.mylibrary.base.BaseActivity
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.permissionx.guolindev.PermissionX

/**
 * @ClassName HomeActivity
 * @Author 用户
 * @Date 2023/12/27 17:12
 * @Description TODO
 */
class HomeActivity: BaseActivity<ActivityHomeBinding>(), OnTabSelectListener,
    ViewPager.OnPageChangeListener {

    private val mNormalRes = mutableListOf(
        R.drawable.tab_home_normal,
        R.drawable.tab_message_normal,
        R.drawable.tab_waybill_normal,
        R.drawable.tab_mine_normal
    )


    private val mSelectRes = mutableListOf(
        R.drawable.tab_home_press,
        R.drawable.tab_message_press,
        R.drawable.tab_waybill_press,
        R.drawable.tab_mine_press
    )
    private  var mTitles = arrayOf("首页","消息","运单","我的")
    private var mTabEntities = arrayListOf<CustomTabEntity>(
        TabBean(mTitles[0],mSelectRes[0],mNormalRes[0]),
        TabBean(mTitles[1],mSelectRes[1],mNormalRes[1]),
        TabBean(mTitles[2],mSelectRes[2],mNormalRes[2]),
        TabBean(mTitles[3],mSelectRes[3],mNormalRes[3]))
    private var mFragments = arrayListOf<Fragment>(HomeFragment(),MessageFragment(), WaybillFragment(),MineFragment())

    override fun initView(savedInstanceState: Bundle?) {

        binding.homeTabLayout.run {
            setTabData(mTabEntities)
            setOnTabSelectListener(this@HomeActivity)
            currentTab=0
        }

        binding.homeNoScrollViewPager.run {
            adapter = MyHomePagerAdapter(this@HomeActivity.supportFragmentManager, mTitles, mFragments)
            addOnPageChangeListener(this@HomeActivity)
            offscreenPageLimit = 3
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            getPermissions()
        }


    }

    override fun initDate() {
    }

    private fun getPermissions() {
        PermissionX.init(this)
            .permissions(
                Manifest.permission.CAMERA)
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {

                } else {
                    Toast.makeText(mContext, "开启权限失败,请在应用设置-权限-定位-始终允许", Toast.LENGTH_SHORT).show()
                }
            }
    }

    override fun onTabSelect(position: Int) {
        binding.homeNoScrollViewPager.setCurrentItem(position)
    }

    override fun onTabReselect(position: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        binding.homeTabLayout.setCurrentTab(position)
    }

    override fun onPageScrollStateChanged(state: Int) {
    }

}