package com.casic.br.view

import android.graphics.Color
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import com.casic.br.R
import com.casic.br.adapter.IconTextListAdapter
import com.casic.br.utils.AuthenticationHelper
import com.casic.br.utils.LocaleConstant
import com.casic.br.vm.UserViewModel
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.convertColor
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.utils.ImmerseStatusBarUtil
import com.pengxh.kt.lite.utils.PageNavigationManager
import com.pengxh.kt.lite.widget.dialog.BottomActionSheet
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.sdk.api.IResultCallback
import kotlinx.android.synthetic.main.activity_settings.*
import kotlinx.android.synthetic.main.include_base_title.*


class SettingsActivity : KotlinBaseActivity() {

    private lateinit var userViewModel: UserViewModel

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

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        ImmerseStatusBarUtil.setColor(this, R.color.white.convertColor(this))

        leftBackView.setOnClickListener { finish() }
        titleView.text = "系统设置"
    }

    override fun initData() {
        userViewModel = ViewModelProvider(this)[UserViewModel::class.java]

        val settingsListAdapter = IconTextListAdapter(this, LocaleConstant.SETTINGS_OPERATE)
        settingsRecyclerView.addItemDecoration(
            DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        )
        settingsRecyclerView.adapter = settingsListAdapter
        settingsListAdapter.setOnGridItemClickListener(object :
            IconTextListAdapter.OnListItemClickListener {
            override fun onItemClick(position: Int) {
                when (position) {
                    0 -> {}
                    1 -> {}
                    2 -> {}
                    3 -> {}
                    4 -> {}
                }
            }
        })
    }

    override fun observeRequestState() {
        userViewModel.outResultModel.observe(this, {
            if (it.code == 200) {
                AuthenticationHelper.removeToken()
                this.navigatePageTo<LoginActivity>()
                PageNavigationManager.finishAllActivity()
            }
        })
    }

    override fun initEvent() {
        loginOutButton.setOnClickListener {
            BottomActionSheet.Builder()
                .setContext(this)
                .setActionItemTitle(arrayListOf("永久退出", "退出当前"))
                .setItemTextColor(Color.BLUE)
                .setOnActionSheetListener(object : BottomActionSheet.OnActionSheetListener {
                    override fun onActionItemClick(position: Int) {
                        when (position) {
                            0 -> TuyaHomeSdk.getUserInstance()
                                .cancelAccount(object : IResultCallback {
                                    override fun onError(code: String?, error: String?) {
                                        Log.d("Casic", "SettingsActivity => onError: $error")
                                    }

                                    override fun onSuccess() {
                                        userViewModel.out()
                                    }
                                })
                            1 -> userViewModel.out()
                        }
                    }
                })
                .build().show()
        }
    }
}