package com.zxhl.main.page.fragment

import android.view.View
import com.zxhl.cms.common.FragmentAdapter
import com.zxhl.cms.common.base.BaseFragment
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.model.box.TabEntity
import com.zxhl.main.R
import com.zxhl.main.page.contract.ShopContract
import com.zxhl.main.page.presenter.ShopPresenter
import com.zxhl.main.page.view.DialogUtils
import kotlinx.android.synthetic.main.fragment_layout_shop.*

class ShopFragment : BaseFragment(), ShopContract.View {

    private var mPresenter: ShopPresenter? = null

    override fun layoutID(): Int {
        return R.layout.fragment_layout_shop
    }

    override fun init(view: View?) {
        mPresenter = ShopPresenter(this)
        mPresenter?.getShopTab()
        shop_viewPager?.currentItem = 1
        val user = SettingPreference.getUserInfoData()
        if (user != null) {
            id_my_mobi_num.text = "${user.avaMagic ?: "0"}"
        }
    }

    override fun setShopTab(result: List<TabEntity>?) {
        if (result != null && result?.size > 0) {
            initVeiwPager(result)
        }
    }

    fun initVeiwPager(result: List<TabEntity>) {
        val adapter = FragmentAdapter(childFragmentManager)
        shop_viewPager?.adapter = adapter

        for (i in 0 until (result?.size ?: 0)) {
            val title = result[i].name ?: ""
            adapter?.addFrag(ShopListFragment.newInstance(title), title)
        }
        adapter?.notifyDataSetChanged()
        id_fragment_shop_tab?.setViewPager(shop_viewPager)
        id_fragment_shop_tab?.overScrollMode = View.OVER_SCROLL_NEVER
    }

    override fun onVisible() {
        super.onVisible()
        if (activity != null && SettingPreference.getFirstShowShopTips()) {
            DialogUtils.showShopTips(activity!!)
            SettingPreference.setFirstShowShopTips()
        }
    }

}