package com.casic.br.fragment

import com.bumptech.glide.Glide
import com.casic.br.R
import com.casic.br.adapter.ConnectGridAdapter
import com.casic.br.adapter.HomeGridAdapter
import com.casic.br.extensions.initLayoutImmersionBar
import com.casic.br.model.BannerImageModel
import com.casic.br.utils.GridItemDecoration
import com.casic.br.utils.LocaleConstant
import com.casic.br.view.LoginActivity
import com.casic.br.view.MainActivity
import com.pengxh.kt.lite.base.KotlinBaseFragment
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.PageNavigationManager
import com.pengxh.kt.lite.utils.SaveKeyValues
import com.pengxh.kt.lite.widget.dialog.AlertControlDialog
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.AlphaPageTransformer
import kotlinx.android.synthetic.main.fragment_home.*
import java.util.*

class HomePageFragment : KotlinBaseFragment() {

    override fun initLayoutView(): Int = R.layout.fragment_home

    override fun setupTopBarLayout() {
        requireContext().initLayoutImmersionBar(rootView)
    }

    override fun initData() {
        //TODO 模拟数据
        val imageModels = ArrayList<BannerImageModel>()
        for (i in 0..4) {
            val model = BannerImageModel()
            model.imageTitle = "测试轮播图"
            model.imageLink = "https://img95.699pic.com/photo/40006/1412.jpg_wh860.jpg"

            imageModels.add(model)
        }

        val banner = homeBannerView
                as Banner<BannerImageModel, BannerImageAdapter<BannerImageModel>>
        banner.apply {
            setAdapter(object : BannerImageAdapter<BannerImageModel>(imageModels) {
                override fun onBindView(
                    holder: BannerImageHolder, data: BannerImageModel, position: Int, size: Int
                ) {
                    Glide.with(holder.itemView)
                        .load(data.imageLink)
                        .placeholder(R.mipmap.load_image_error)
                        .into(holder.imageView)
                }
            })
            indicator = CircleIndicator(requireContext())
            addPageTransformer(AlphaPageTransformer())
            addBannerLifecycleObserver(this@HomePageFragment)

            //Banner点击事件
            setOnBannerListener { imageModel, _ ->
                "${imageModel.imageTitle}被点击了".show(requireContext())
            }
        }

        val homeAdapter = HomeGridAdapter(requireContext(), LocaleConstant.HOME_FUNC_OPERATE)
        homeRecyclerView.addItemDecoration(GridItemDecoration(requireContext()))
        homeRecyclerView.adapter = homeAdapter
        homeAdapter.setOnGridItemClickListener(object : HomeGridAdapter.OnGridItemClickListener {
            override fun onItemClick(position: Int) {

            }
        })

        val connectGridAdapter =
            ConnectGridAdapter(
                requireContext(), LocaleConstant.CONNECT_IMAGE, LocaleConstant.CONNECT_ARRAY
            )
        connectRecyclerView.addItemDecoration(GridItemDecoration(requireContext()))
        connectRecyclerView.adapter = connectGridAdapter
        homeAdapter.setOnGridItemClickListener(object : HomeGridAdapter.OnGridItemClickListener {
            override fun onItemClick(position: Int) {

            }
        })
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        rightOperateView.setOnClickListener {
            if (rightOperateView.text == "退出") {
                AlertControlDialog.Builder()
                    .setContext(requireContext())
                    .setTitle("退出确认")
                    .setMessage("退出当前账号，将不能同步数据")
                    .setNegativeButton("取消")
                    .setPositiveButton("确认退出")
                    .setOnDialogButtonClickListener(object :
                        AlertControlDialog.OnDialogButtonClickListener {
                        override fun onCancelClick() {

                        }

                        override fun onConfirmClick() {
                            SaveKeyValues.putValue(LocaleConstant.USER_IS_LOGIN, false)
                            requireContext().navigatePageTo<MainActivity>()
                            PageNavigationManager.finishAllActivity()
                        }
                    })
                    .build().show()
            } else {
                requireContext().navigatePageTo<LoginActivity>()
            }
        }
    }

    override fun onResume() {
        val isLogin = SaveKeyValues.getValue(LocaleConstant.USER_IS_LOGIN, false) as Boolean
        if (isLogin) {
            rightOperateView.text = "退出"
        } else {
            rightOperateView.text = "登录"
        }
        super.onResume()
    }
}