package com.casic.br.fragment

import androidx.lifecycle.ViewModelProvider
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.combineImagePath
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.LoginActivity
import com.casic.br.view.SettingsActivity
import com.casic.br.vm.UserViewModel
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.extensions.toJson
import com.pengxh.kt.lite.utils.SaveKeyValues
import kotlinx.android.synthetic.main.fragment_mine.*
import kotlinx.android.synthetic.main.include_main_title.*

class MinePageFragment : KotlinBaseFragment() {

    private lateinit var userViewModel: UserViewModel

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

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

    override fun initData() {
        userViewModel = ViewModelProvider(this)[UserViewModel::class.java]
        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 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    1 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    2 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                }
            }
        })

        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 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    1 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    2 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    3 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                }
            }
        })

        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 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    1 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    2 -> {
                        "此功能待后续完善".show(requireContext())
                    }
                    //地址管理
                    3 -> {
                        if (DeserializeModel.isLogin()) {
                            requireContext().navigatePageTo<AddressManagerActivity>()
                        } else {
                            requireContext().navigatePageTo<LoginActivity>()
                        }
                    }
                    //系统设置
                    4 -> {
                        if (DeserializeModel.isLogin()) {
                            requireContext().navigatePageTo<SettingsActivity>()
                        } else {
                            requireContext().navigatePageTo<LoginActivity>()
                        }
                    }
                }
            }
        })
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        editUserView.setOnClickListener {
            if (DeserializeModel.isLogin()) {
                requireContext().navigatePageTo<EditUserActivity>()
            } else {
                requireContext().navigatePageTo<LoginActivity>()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (DeserializeModel.isLogin()) {
            val userModel = DeserializeModel.getUserModel()!!
            userViewModel.obtainUserDetail(userModel.account)
            userViewModel.userDetailResult.observe(this, { user ->
                if (user.code == 200) {
                    SaveKeyValues.putValue(LocaleConstant.USER_MODEL, user.toJson())
                    val currentUser = user.data
                    if (currentUser.avatar == "") {
                        Glide.with(this)
                            .load(R.mipmap.ic_launcher)
                            .apply(RequestOptions.circleCropTransform())
                            .placeholder(R.mipmap.load_image_error)
                            .into(userAvatarView)
                    } else {
                        Glide.with(this)
                            .load(currentUser.avatar.combineImagePath())
                            .apply(RequestOptions.circleCropTransform())
                            .placeholder(R.mipmap.load_image_error)
                            .into(userAvatarView)
                    }
                    userNameView.text = currentUser.name
                    userPhoneView.text = currentUser.phone
                }
            })
        } else {
            Glide.with(this)
                .load(R.mipmap.ic_launcher)
                .apply(RequestOptions.circleCropTransform())
                .placeholder(R.mipmap.load_image_error)
                .into(userAvatarView)
            userNameView.text = "未登录"
            userPhoneView.text = ""
        }
    }
}