package com.android.launcher3.function.setting

import android.content.Context
import android.icu.lang.UCharacter
import android.os.Bundle
import android.view.View
import android.view.View.VISIBLE
import com.android.launcher3.BaseActivity
import com.android.launcher3.R
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView
import com.secspace.lib.common.dialog.LoadingDialog
import com.secspace.lib.common.dialog.other.DialogUIUtils
import com.secspace.lib.common.function.ActivationHelper
import com.secspace.lib.common.function.ActivationHelper.ActivationListener
import com.secspace.lib.common.utils.ImeiUtil
import com.secspace.lib.common.utils.dp2px
import kotlinx.android.synthetic.main.activity_setting.*

class SettingBuyActivity : BaseActivity() {
    private var activationType: QMUICommonListItemView? = null
    private var code: QMUICommonListItemView? = null
    private var timeStart: QMUICommonListItemView? = null
    private var timeEnd: QMUICommonListItemView? = null
    private var uniqueId: QMUICommonListItemView? = null
    private lateinit var mContext: Context

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        setAndroidNativeLightStatusBar(true)
        setContentView(R.layout.activity_setting)
        initView()
    }

    private fun initView() {
        parent_buy.visibility = VISIBLE
        tv_setting_title.setText(R.string.edu_item_buy)
        activationType = grouplistview.createItemView(null,
                getString(R.string.edu_item_activation_type),
                ActivationHelper.getActivationType(mContext),
                UCharacter.DecompositionType.VERTICAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE,
                dp2px(48f))
        code = grouplistview.createItemView(null,
                getString(R.string.edu_item_activation_code),
                ActivationHelper.getActivationCode(mContext),
                UCharacter.DecompositionType.VERTICAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE,
                dp2px(48f))
        timeStart = grouplistview.createItemView(null,
                getString(R.string.edu_item_activation_start),
                ActivationHelper.getActivationTimeStart(mContext),
                UCharacter.DecompositionType.VERTICAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE,
                dp2px(48f))
        timeEnd = grouplistview.createItemView(null,
                getString(R.string.edu_item_activation_end),
                ActivationHelper.getActivationTimeEnd(mContext),
                UCharacter.DecompositionType.VERTICAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE,
                dp2px(48f))
        uniqueId = grouplistview.createItemView(null,
                getString(R.string.edu_item_unique),
                ImeiUtil.uniqueId(mContext, true),
                UCharacter.DecompositionType.VERTICAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE,
                dp2px(48f))
        QMUIGroupListView.newSection(mContext)
                .addItemView(activationType) {}
                .addItemView(code) { }
                .addItemView(timeStart) {}
                .addItemView(timeEnd) { }
                .addItemView(uniqueId) { }
                .addTo(grouplistview)
        btn_activation.setOnClickListener {
            LoadingDialog.getInstance(mContext).show("请稍后")
            ActivationHelper.active(this, object : ActivationListener {
                override fun activated() {
                    LoadingDialog.getInstance(mContext).dismiss()
                    DialogUIUtils.showToastCenter("该设备已激活")
                }

                override fun activationFailed() {
                    LoadingDialog.getInstance(mContext).dismiss()
                }
            }, true)
        }
    }

    override fun onPause() {
        super.onPause()
        LoadingDialog.getInstance(mContext).dismiss()
    }

    fun close(view: View?) {
        finish()
    }
}