package com.dahe.gldriver.ui.mine

import android.os.Bundle
import android.view.View
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.GridItemAdapter
import com.dahe.gldriver.base.WebActivity
import com.dahe.gldriver.bean.GridBean
import com.dahe.gldriver.databinding.FragmentMineBinding
import com.dahe.gldriver.ui.account.authperson.AuthPartyActivity
import com.dahe.gldriver.ui.mine.activity.CarTeamActivity
import com.dahe.gldriver.ui.mine.activity.CarsManActivity
import com.dahe.gldriver.ui.mine.activity.DriverTeamsActivity
import com.dahe.gldriver.ui.mine.activity.PersonInfoActivity
import com.dahe.gldriver.ui.mine.activity.RateManActivity
import com.dahe.gldriver.ui.mine.activity.WalletActivity
import com.dahe.gldriver.utils.SPUtils
import com.dahe.mylibrary.base.BaseFragment
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.BaseUtils
import com.dahe.mylibrary.utils.ImageLoader
import com.dahe.mylibrary.utils.PhoneFormatCheckUtils
import com.dahe.mylibrary.utils.ToastUtils
import kotlin.system.exitProcess

class MineFragment : BaseFragment<FragmentMineBinding>(), View.OnClickListener {
    private val tools = mutableListOf(
        GridBean("车队管理", R.drawable.car_team),
        GridBean("车辆管理", R.drawable.car_manage),
        GridBean("司机课堂", R.drawable.mine_class),
        GridBean("评价管理", R.drawable.rate)
    )

    private val plats = mutableListOf(
        GridBean("平台规则", R.drawable.rules),
        GridBean("常见问题", R.drawable.question),
        GridBean("联系我们", R.drawable.call_us),
        GridBean("意见反馈", R.drawable.opin),
        GridBean("红色方向盘", R.drawable.part_dir),
        GridBean("检查更新", R.drawable.update),
        GridBean("关于我们", R.drawable.about),
        GridBean("设置", R.drawable.setting)
    )

    override fun onFragmentVisibleChange(isVisible: Boolean) {
        if (isVisible) {
        } else {
        }
    }


    override fun onFragmentFirstVisible() {

        val userInfo = SPUtils.instance.getUserInfo(mContext)
        if (userInfo != null) {
            binding.run {
                tvName.text = userInfo.idcardName
                tvCarType.text = userInfo.approvedType
                tvPhone.text = PhoneFormatCheckUtils.hintPhone(userInfo.contactPhone)
                tvCarType.visibility =
                    if (userInfo.approvedType.isNullOrEmpty()) View.GONE else View.VISIBLE
                ivPart.visibility =
                    if (userInfo.partyMemberCertification == "1") View.VISIBLE else View.GONE
                ImageLoader.getInstance()
                    .loadCircleImage(mContext, userInfo.avatar, ivHead, R.drawable.head_defaut)
            }
        }

        binding.run {
            //初始化常用工具
            gvTools.run {
                adapter = GridItemAdapter(mContext, tools, R.layout.grid_mine_item)
                setOnItemClickListener { _, _, i, _ ->
                    goTools(i)
                }
            }
            //初始化平台规则
            gvPlat.run {
                adapter = GridItemAdapter(mContext, plats, R.layout.grid_mine_item)
                setOnItemClickListener { _, _, i, _ ->
                    goPlats(i)
                }
            }

            rlMoney.setOnClickListener {
                ActivityUtils.startActivity(mContext, WalletActivity::class.java)
            }

            llPerson.setOnClickListener {
                ActivityUtils.startActivity(mContext, PersonInfoActivity::class.java)
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
//            R.id.rlPass->ActivityUtils.startActivity(mContext,ChangePwdActivity::class.java)
//            R.id.rlPhone-> AppActivityUtils.openWebViewActivity(mContext, "隐私政策",
//                "http://tms.down.user.test.dahehuodongbao.com/user-privacy.html")
////            R.id.rlPhone->ActivityUtils.startActivity(mContext,ChangePhoneActivity::class.java)
//            R.id.btnOut-> CrashReport.testJavaCrash();
        }
    }

    fun logOut() {
//        UserUtils.instance.loginOut(requireActivity())
//        val intent = Intent(mContext, LoginActivity::class.java)
//        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
//        requireActivity().startActivity(intent)
    }

    /**
     * 常用工具跳转逻辑
     */
    private fun goTools(pos: Int) {
        when (pos) {
            0 -> {
                if (SPUtils.instance.getUserInfo(mContext)?.isCarCaptain == "1") {//车队长
                    ActivityUtils.startActivity(mContext, CarTeamActivity::class.java)
                } else {//司机
                    ActivityUtils.startActivity(mContext, DriverTeamsActivity::class.java)
                }


            }

            1 -> {
                ActivityUtils.startActivity(mContext, CarsManActivity::class.java)
            }

            2 -> {
                var bundle = Bundle()
                bundle.putString("title", "我的百度")
                bundle.putString("url", "www.baidu.com")
                ActivityUtils.startActivity(mContext, WebActivity::class.java, bundle)
                ToastUtils.showToast(mContext, tools[pos].title)
            }

            3 -> {
                ActivityUtils.startActivity(mContext,RateManActivity::class.java)
            }

        }
    }

    /**
     * 平台规则跳转逻辑
     */
    private fun goPlats(pos: Int) {
        when (pos) {
            0 -> {
                ActivityUtils.startActivity(mContext, CarTeamActivity::class.java)
            }

            1 -> {
                ActivityUtils.startActivity(mContext, CarsManActivity::class.java)
            }

            2 -> {
                ToastUtils.showToast(mContext, plats[pos].title)
            }

            3 -> {
                ToastUtils.showToast(mContext, plats[pos].title)
            }

            4 -> {
                ActivityUtils.startActivity(mContext, AuthPartyActivity::class.java)
            }

            5 -> {
                ToastUtils.showToast(mContext, plats[pos].title)
            }

            6 -> {
                ToastUtils.showToast(mContext, plats[pos].title)
            }

            7 -> {
                SPUtils.instance.removeUserToken(mContext)
                ActivityUtils.finishAllActivities()
                showToast("清除token")
//                exitProcess(0)
            }

        }
    }


}