package com.dahe.pass.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.VersionUtils
import com.dahe.pass.R
import com.dahe.pass.adapter.MyHomePagerAdapter
import com.dahe.pass.bean.TabBean
import com.dahe.pass.net.Api
import com.dahe.pass.ui.gone.GoneFragment
import com.dahe.pass.ui.home.HomeFragment
import com.dahe.pass.ui.mine.MineFragment
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.umeng.message.PushAgent
import kotlinx.android.synthetic.main.activity_home.*

import java.util.*

class HomeActivity : BaseActivity(), ViewPager.OnPageChangeListener, OnTabSelectListener {

    val home: String = "申请记录"
    val waybill: String = "已使用"
    val mine: String = "我的"

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


    private val mSelectRes = mutableListOf(
        R.drawable.tab_home_press,
        R.drawable.tab_waybill_press,
        R.drawable.tab_mine_press
    )
    private lateinit var mTitles: Array<String>
    private var mTabEntities = ArrayList<CustomTabEntity>()
    private var mFragments = ArrayList<Fragment>()


    override fun getLayout() = R.layout.activity_home

    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarCancelLightColor()
        mFragments.add(HomeFragment())
        mFragments.add(GoneFragment())
        mFragments.add(MineFragment())
        mTitles = arrayOf(home, waybill, mine)

        for (i in mTitles.indices) {
            mTabEntities.add(TabBean(mTitles[i], mSelectRes[i], mNormalRes[i]))
        }



        home_tabLayout.setTabData(mTabEntities)
        val intent = intent
//        var intExtra = 0
//        if (intent != null) {
//            intExtra = intent.getIntExtra(AppConfig.CURR_PAGE, 0)
//        }

        home_tabLayout.setOnTabSelectListener(this)
        home_NoScrollViewPager.setAdapter(MyHomePagerAdapter(this.supportFragmentManager,
            mTitles,
            mFragments))
        home_NoScrollViewPager.addOnPageChangeListener(this)
        home_NoScrollViewPager.setOffscreenPageLimit(3)

        home_NoScrollViewPager.setCurrentItem(0)
        home_tabLayout.setCurrentTab(0)

        checkVersion()
    }

    override fun initDate() {
//        val extras = intent.extras
//        if (extras?.getBoolean(AppConfig.IS_MESSAGE, false) == true) {
//            val msgType = extras.getString(AppConfig.MESSAGE_TYPE)
//            if ("0" != msgType){
//                ActivityUtils.startActivity(this, WarnDetailActivity::class.java, extras)
//            }else{
//                ActivityUtils.startActivity(this, OrderDetailActivity::class.java, extras)
//            }
//
//        }
    }

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

    override fun onPageSelected(position: Int) {
        home_tabLayout.setCurrentTab(position)

    }

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onTabSelect(position: Int) {

//        if (SPUtils.hasUserInfo(App.getApp())) {
        home_NoScrollViewPager.setCurrentItem(position)
    }

    override fun onTabReselect(position: Int) {
    }

    /**
     * 版本检查
     */
    private fun checkVersion() {
        VersionUtils.getInstance().checkVersion(this, Api.VERSION, false)
    }


    private var mBackKeyPressed = false
    var isCheckVer = false

    /**
     * 第四种方法
     */
    override fun onBackPressed() {
        if (!mBackKeyPressed) {
            showToast("再按一次退出程序")
            mBackKeyPressed = true
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    mBackKeyPressed = false
                }
            }, 2000)
        } else {
            isCheckVer = true
            canCelToast()
            moveTaskToBack(true)
//            finish()
        }
    }

    override fun onRestart() {
        super.onRestart()
//        if (isCheckVer) {
//            checkVersion()
//            isCheckVer = false
//        }
    }
//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        if (resultCode == RESULT_OK){
//            val stringExtra = data?.getStringExtra(AppConfig.BEANS)
//            if (null!=stringExtra){
//                val fromJson =
//                    JsonUtils.getInstance().fromJson(stringExtra, WayBillSearchBean::class.java)
//            }
//        }
//    }


}