package com.casic.br.fragment

import androidx.recyclerview.widget.DividerItemDecoration
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.casic.br.R
import com.casic.br.adapter.IconTextGridAdapter
import com.casic.br.adapter.IconTextListAdapter
import com.casic.br.extensions.initLayoutImmersionBar
import com.casic.br.utils.DeserializeModel
import com.casic.br.utils.LocaleConstant
import com.casic.br.view.AddressManagerActivity
import com.casic.br.view.EditUserActivity
import com.casic.br.view.SettingsActivity
import com.pengxh.kt.lite.base.KotlinBaseFragment
import com.pengxh.kt.lite.extensions.navigatePageTo
import kotlinx.android.synthetic.main.fragment_mine.*
import kotlinx.android.synthetic.main.include_main_title.*

class MinePageFragment : KotlinBaseFragment() {

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

    override fun setupTopBarLayout() {
        requireContext().initLayoutImmersionBar(rootView)
        leftTitleView.text = "我的"
    }

    override fun initData() {
        val shopGridAdapter = IconTextGridAdapter(
            requireContext(),
            LocaleConstant.SHOP_IMAGE,
            LocaleConstant.SHOP_OPERATE
        )
        shopRecyclerView.adapter = shopGridAdapter
        shopGridAdapter.setOnGridItemClickListener(object :
            IconTextGridAdapter.OnGridItemClickListener {
            override fun onItemClick(position: Int) {
                when (position) {
                    0 -> {}
                    1 -> {}
                    2 -> {}
                }
            }
        })

        val serverGridAdapter = IconTextGridAdapter(
            requireContext(),
            LocaleConstant.SERVER_IMAGE,
            LocaleConstant.SERVER_OPERATE
        )
        serverRecyclerView.adapter = serverGridAdapter
        serverGridAdapter.setOnGridItemClickListener(object :
            IconTextGridAdapter.OnGridItemClickListener {
            override fun onItemClick(position: Int) {
                when (position) {
                    0 -> {}
                    1 -> {}
                    2 -> {}
                    3 -> {}
                }
            }
        })

        val settingsListAdapter = IconTextListAdapter(
            requireContext(), LocaleConstant.USER_OPERATE
        )
        settingsRecyclerView.addItemDecoration(
            DividerItemDecoration(
                requireContext(),
                DividerItemDecoration.VERTICAL
            )
        )
        settingsRecyclerView.adapter = settingsListAdapter
        settingsListAdapter.setOnGridItemClickListener(object :
            IconTextListAdapter.OnListItemClickListener {
            override fun onItemClick(position: Int) {
                when (position) {
                    0 -> {}
                    1 -> {}
                    2 -> {}
                    //地址管理
                    3 -> requireContext().navigatePageTo<AddressManagerActivity>()
                    //系统设置
                    4 -> requireContext().navigatePageTo<SettingsActivity>()
                }
            }
        })
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        editUserView.setOnClickListener {
            requireContext().navigatePageTo<EditUserActivity>()
        }
    }

    override fun onResume() {
        super.onResume()
        val userModel = DeserializeModel.getUserModel()
        if (userModel == null) {
            Glide.with(this)
                .load(R.mipmap.ic_launcher)
                .apply(RequestOptions.circleCropTransform())
                .placeholder(R.mipmap.load_image_error)
                .into(userAvatarView)
            userNameView.text = "未登录"
            userPhoneView.text = ""
            return
        }
        if (userModel.headPic == "") {
            Glide.with(this)
                .load(R.mipmap.ic_launcher)
                .apply(RequestOptions.circleCropTransform())
                .placeholder(R.mipmap.load_image_error)
                .into(userAvatarView)
        } else {
            Glide.with(this)
                .load(userModel.headPic)
                .apply(RequestOptions.circleCropTransform())
                .placeholder(R.mipmap.load_image_error)
                .into(userAvatarView)
        }
        userNameView.text = requireContext().resources.getString(R.string.app_name) +
                userModel.username.subSequence(10, 14)
        userPhoneView.text = userModel.mobile
    }
}