package com.android.launcher3.function.setting

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.GridLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.android.launcher3.BaseActivity
import com.android.launcher3.R
import com.android.launcher3.mdm.DisableCompat
import com.android.launcher3.settings.Contants
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView
import com.secspace.lib.common.dialog.SwitcherDialog
import com.secspace.lib.common.event.ChangeWallPaperEvent
import com.secspace.lib.common.utils.PkgManager
import com.secspace.lib.common.utils.Prefs
import com.secspace.lib.common.utils.ToastUtil
import com.secspace.lib.common.utils.UninstallUtil.removeForbidden
import com.secspace.lib.common.utils.UninstallUtil.uninstallService
import com.secspace.lib.common.utils.dp2px
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

class SettingPwdActivity : BaseActivity(){
    private var mContext: Context? = null
    private var pwd: QMUICommonListItemView? = null
    private var pwdCar: QMUICommonListItemView? = null

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

    private fun initView() {
        tv_setting_title.setText(R.string.edu_item_pwd_manager)
        pwd = grouplistview.createItemView(null,
                getString(R.string.edu_item_change_pwd),
                null,
                GridLayout.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON,
                dp2px(48f))
        pwdCar = grouplistview.createItemView(null,
                getString(R.string.edu_item_pwd_cer),
                null,
                GridLayout.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON,
                dp2px(48f))
        QMUIGroupListView.newSection(this)
                .addItemView(pwd) {
                    val intent1 = Intent(this, ChangePwdActivity::class.java)
                    intent1.putExtra(Contants.IS_SHOW_ORIGINAL_PWD, true)
                    startActivity(intent1)
                }
                .addItemView(pwdCar) {
                    ToastUtil.showShort(mContext, R.string.edu_toast_wait)
                }
                .addTo(grouplistview)
    }

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

    private fun reinforce() {
        val packages: MutableList<String> = PkgManager.getInstance(mContext).queryLauncherPackages()
        packages.remove(mContext?.packageName)
        if(packages.isNotEmpty()){
            Prefs.setHideLauncherPackages(mContext, packages)
        }
        for (packageName in packages) {
            DisableCompat.disableApplication(mContext, packageName)
        }
    }

    private fun uninstall() {
        GlobalScope.launch(Dispatchers.Main) {
            SwitcherDialog.getInstance(mContext).show(getString(R.string.edu_uninstall), "unInstall")
            removeForbidden(mContext)
            delay(1000)
            uninstallService(mContext)
            delay(800)
            SwitcherDialog.getInstance(mContext).dismiss("unInstall")
        }
    }

    override fun onActivityResult(
            requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_PICK_WALLPAPER) {
            if (resultCode == Activity.RESULT_OK) {
                Log.e("WWWW", "AAAAA")
                val selectedImage = data!!.data
                EventBus.getDefault().postSticky(ChangeWallPaperEvent(selectedImage))
                ToastUtil.showShort(this, "壁纸修改成功")
            }
        }
    }

    private inline fun showTipDialog(crossinline block: () -> Unit, text: String) {
        val alertDialog: AlertDialog
        val textView = TextView(this)
        val view = LayoutInflater.from(this).inflate(R.layout.item_ll, null)
        textView.text = text
        val ll = view.findViewById<LinearLayout>(R.id.item_ll)
        ll.addView(textView)
        alertDialog = AlertDialog.Builder(this)
                .setTitle(getString(R.string.tip))
                .setCancelable(false)
                .setView(view)
                .setNegativeButton(R.string.work_cancel) { dialog, _ -> dialog.dismiss() }
                .setPositiveButton(R.string.work_sure) { dialog, _ ->
                    run {
                        dialog.dismiss()
                        block()
                    }
                }
                .create()
        if (!alertDialog.isShowing) {
            alertDialog.show()
        }
    }

    companion object {
        private const val REQUEST_PICK_WALLPAPER = 10
        private const val ACTION_CHANGE_WALLPAPER = "action_change_wallpaper"
        //允许添加先安装应用
        const val ALLOW_NEW_APP = 0
        //允许本身自启
        const val AUTO_START_SELF = 1
    }
}