package com.zswc.creativity.activity

import android.graphics.Color
import android.os.Bundle
import android.view.KeyEvent
import androidx.core.content.ContextCompat
import com.githang.statusbar.StatusBarCompat
import com.zswc.creativity.R
import com.zswc.creativity.base.MBActivity
import com.zswc.creativity.databinding.ActivityMainBinding
import com.zswc.creativity.fragment.HomeFirstFragment
import com.zswc.creativity.fragment.HomeMyFragment
import com.zswc.creativity.vmodel.MainVModel
import com.ysnows.base.utils.KeySimulateUtils
import com.ysnows.update.component.UpdateUtils
import com.zswc.creativity.fragment.ConSaleFragmet
import com.zswc.creativity.fragment.ShopListFragmet
import java.util.*

/**
 *Created by shihuiyun
 *on 2020/12/4
 */
class MainActivity : MBActivity<MainVModel, ActivityMainBinding>(){


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#00ffffff"))
        initData()
        //检查更新
        UpdateUtils.checkUpgrade(false,true)


    }

    private fun initData() {
        initMainMenu()
        binding.mainMenu.switchTab(0)

    }

    fun initMainMenu() {

        if (null == binding.mainMenu) {
            return
        }

        val menuTabBuilder = binding.mainMenu.tabBuilder()
                .setSelectColor(Color.parseColor("#3AD3D5"))
                .setNormalColor(Color.parseColor("#222222"))


        val oneFragment = HomeFirstFragment()
        val twoFragment = ConSaleFragmet()
         val threeFragment = ShopListFragmet()
        val fourFragment = HomeMyFragment()
        //  fourFragment.arguments = B().putString("type", type).ok()

        val one = menuTabBuilder
                .setFragment(oneFragment)
                .setNormalDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_one))
                .setSelectedDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_one_focus))
                .setText("首页")
                .build(context())
        val two = menuTabBuilder
                .setFragment(twoFragment)
                .setNormalDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_two))
                .setSelectedDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_two_focus))
                .setText("寄售")
                .build(context())
        val three = menuTabBuilder
                .setFragment(threeFragment)
                .setText("积分商城")
                .setNormalDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_three))
                .setSelectedDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_three_focus))
                .build(context())
        val four = menuTabBuilder
                .setFragment(fourFragment)
                .setText("我的")
                .setNormalDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_four))
                .setSelectedDrawable(ContextCompat.getDrawable(context(), R.drawable.menu_four_focus))
                .build(context())

        binding.mainMenu
                .addTab(one)
                .addTab(two)
                .addTab(three)
                .addTab(four)
                .rendor()


//        binding.mainMenu.setListener {
//            if(it == "3"){
//                RxBus.get().post(ConfigRx.REFRESH_USER_DATA, "")
//
//            }
//        }


    }



    override fun listeners() {
        super.listeners()

    }

    override fun binding(): ActivityMainBinding {
        return ActivityMainBinding.inflate(layoutInflater)
    }

    override fun transluent(): Boolean {
        return true
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true

    /**
     * 双击退出函数
     */
    private var isExit = false

    /**
     * 菜单、返回键响应
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click() //调用双击退出函数
            return true
        }
        return false
    }

    private fun exitBy2Click() {
        var tExit: Timer? = null
        if (!isExit) {
            isExit = true // 准备退出
            toast("再按一次退出应用")
            tExit = Timer()
            tExit.schedule(object : TimerTask() {
                override fun run() {
                    isExit = false // 取消退出
                }
            }, 1500) // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            KeySimulateUtils.simulate(KeyEvent.KEYCODE_HOME)
            System.exit(0)
        }
    }



}

