package cn.muchinfo.rma.view


import android.graphics.Color
import android.os.Bundle
import android.view.View
import cn.muchinfo.rma.R
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.*
import cn.muchinfo.rma.view.base.BaseActivity
import cn.muchinfo.rma.view.base.exposure.ExposureFragment
import cn.muchinfo.rma.view.base.future.FutureFragment
import cn.muchinfo.rma.view.base.home.HomeFragment
import cn.muchinfo.rma.view.base.procurement.ProcurementFragment
import cn.muchinfo.rma.view.base.sale.SaleFragment
import cn.muchinfo.rma.view.eventbus.EventConstent
import cn.muchinfo.rma.view.eventbus.MessageEvent
import com.blankj.utilcode.util.AppUtils
import com.facebook.drawee.backends.pipeline.Fresco
import com.next.easynavigation.view.EasyNavigationBar
import mtp.polymer.com.autowidget.dialog.createLoadingDialog
import mtp.polymer.com.autowidget.dialog.createWarningDialog
import mtp.polymer.com.autowidget.utils.bindTaskStatus
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.*


class MainActivity : BaseActivity<MainViewModel>() {

    /** 首页 **/
    val homeFragment by lazy {
        HomeFragment()
    }

    /** 采购页面 **/
    val procurementFragment by lazy {
        ProcurementFragment()
    }

    /** 销售页面 **/
    val saleFragment by lazy {
        SaleFragment()
    }

    /** 期货 **/
    val futureFragment by lazy {
        FutureFragment()
    }

    /** 敞口 ***/
    val exposureFragment by lazy {
        ExposureFragment()
    }

    /** 状态控制页 **/
    private lateinit var statusLayout: StatusLayout

    private val fragments by lazy {
        listOf(
            homeFragment,
            procurementFragment,
            saleFragment, futureFragment, exposureFragment
        )
    }

    /**
     * 首页底部tab文字
     */
    private val tabText =
        arrayOf("首页", "采购", "销售", "期货", "敞口")

    //未选中icon
    private val normalIcon =
        intArrayOf(
            R.mipmap.main_tab_1,
            R.mipmap.main_tab_2,
            R.mipmap.main_tab_3,
            R.mipmap.main_tab_4,
            R.mipmap.main_tab_5
        )

    //选中时icon
    private val selectIcon =
        intArrayOf(
            R.mipmap.rma_select_main_tab_1,
            R.mipmap.rma_select_main_tab_2,
            R.mipmap.rma_select_main_tab_3,
            R.mipmap.rma_select_main_tab_4,
            R.mipmap.rma_select_main_tab_5
        )

    /**
     * 初始化数据请求
     */
    fun initQuery() {
        viewModel.initiaData()
    }

    private val dialog by lazy { createLoadingDialog(hintStr = "请求中...") }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Fresco.initialize(this)
        EventBus.getDefault().register(this)
        verticalLayout {
            initQuery()
            dialog.bindTaskStatus(context, viewModel.loadingDialogStatus)
            viewModel.isInitiaDataSuccess.bindOptional(context) {
                if (it == false) {
                    this@MainActivity.createWarningDialog {
                        setTitle("温馨提示")
                        setMessage("数据初始化失败")
                        addAction("重新加载") { dialog, _ ->
                            viewModel.initiaData()
                            dialog.dismiss()
                        }
                        addAction("重新登陆") { dialog, _ ->
                            AppUtils.relaunchApp()
                            dialog.dismiss()
                        }
                    }.show()
                }else{
                    EventBus.getDefault().post(MessageEvent(EventConstent.USERACCOUNTCHANGE))
                }
            }
            background = resources.getDrawable(R.color.white)
            easyNavigationBar {
                this.defaultSetting()
                    .titleItems(tabText)
                    .normalIconItems(normalIcon)   //  Tab未选中图标集合
                    .selectIconItems(selectIcon)   //  Tab选中图标集合
                    .fragmentList(fragments)       //  fragment集合
                    .fragmentManager(supportFragmentManager)
                    .iconSize(20f)     //Tab图标大小
                    .tabTextSize(10)   //Tab文字大小
                    .tabTextTop(2)     //Tab文字距Tab图标的距离
                    .normalTextColor(Color.parseColor("#8F8AA6"))   //Tab未选中时字体颜色
                    .selectTextColor(Color.parseColor("#2481DD"))   //Tab选中时字体颜色
                    .smoothScroll(true)  //点击Tab  Viewpager切换是否有动画
                    .canScroll(false)    //Viewpager能否左右滑动
                    .setOnTabClickListener(object : EasyNavigationBar.OnTabClickListener{
                        override fun onTabSelectEvent(view: View?, position: Int): Boolean {
                             // FIXME 后期在此处添加首页滑动到顶部处理
                            return false
                        }

                        override fun onTabReSelectEvent(view: View?, position: Int): Boolean {

                            return false
                        }

                    })
                    .build()
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(messageEvent : MessageEvent) {

    }


}