/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.sybotan.android.works.layouts

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.design.widget.BottomNavigationView
import android.util.AttributeSet
import android.view.*
import android.widget.TextView
import com.sybotan.android.base.extensions.indexOf
import com.sybotan.android.views.adapters.SPagerAdapter
import com.sybotan.android.works.R
import kotlinx.android.synthetic.main.layout_bottom_navigation.view.*
import kotlin.math.max

/**
 * 底部导航布局
 *
 * @author  庞利祥(sybotan@126.com)
 */
open class SBottomNavigationLayout(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) {
    /** 视图列表 */
    private val viewList = arrayListOf<View>()
    /** 当前菜单项 */
    private var menuItem: MenuItem? = null

    /** 底部导航项目变更监听器 */
    private val navigationItemSelectedListener = NavigationItemSelectedListener()

    /** 实例化View回调 */
    var instantiateViewCallback: ((postion: Int) -> View?) ?  = null

    /** 初始化 */
    init {
        // 加载布局文件
        LayoutInflater.from(context).inflate(R.layout.layout_bottom_navigation, this)

        // 处理自定义属性
        parserAttrs(attrs)

        // 设置UI
        setupUi()
    } // init

    /**
     * 设置UI
     */
    private fun setupUi() {
        viewPager.adapter = ViewPagerAdapter()
        viewPager.isSlide = false

        // 关闭导航条Shift模式
        navMain.labelVisibilityMode = 1
        navMain.setOnNavigationItemSelectedListener(navigationItemSelectedListener)
        return
    } // Function setupUi()

    /**
     * 处理自定义属性
     *
     * @param   attrs       自定义属性列表
     */
    private fun parserAttrs(attrs: AttributeSet?) {
        try {
            val t = context.obtainStyledAttributes(attrs, R.styleable.SBottomNavigationLayout)
            val menuId = t.getResourceId(R.styleable.SBottomNavigationLayout_nav_menu, 0)
            t.recycle()
            if (menuId != 0) {
                navMain.inflateMenu(menuId)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return
    } // Function parserAttrs()

    /**
     * View页面适配器
     *
     * @author  庞利祥(sybotan@126.com)
     */
    inner class ViewPagerAdapter : SPagerAdapter() {

        /** 构造函数 */
        init {
            //viewList.add(MyPaper(this@MainActivity))
        } // init

        /**
         * 获得列表项数量
         *
         * @return  列表项数量
         */
        override fun getCount(): Int {
            return navMain.menu.size()
        } // Function getCount()

        /**
         * 实例化列表项视图
         *
         * @param   container   容器对象
         * @param   position    数据项索引
         * @return  列表项视图
         */
        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            return try {
                val itemView = instantiateViewCallback?.invoke(position)!!
                container.addView(itemView)
                itemView
            } catch (e: Exception) {
                val view = TextView(context)
                view.text = "$position"
                container.addView(view)
                view
            }
        } // Function instantiateItem()
    } // Class MainPagerAdapter

    /**
     * 底部导航项目变更监听器
     *
     * @author  庞利祥(sybotan@126.com)
     */
    inner class NavigationItemSelectedListener : BottomNavigationView.OnNavigationItemSelectedListener {
        override fun onNavigationItemSelected(item: MenuItem): Boolean {
            val index = navMain.menu.indexOf(item)
            viewPager.currentItem = max(index, 0)
            return true
        } // Function onNavigationItemSelected()
    } // Class  NavigationItemSelectedListener
} // Class SBottomNavigationLayout

