package com.quansu.heifengwuliu.activity

import android.content.Intent
import android.view.View
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialog.MessageDialogBuilder
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.base.MBActivity
import com.quansu.heifengwuliu.config.ConfigRx
import com.quansu.heifengwuliu.databinding.ActivitySettingsBinding
import com.quansu.heifengwuliu.repository.UserInfoRepository
import com.quansu.heifengwuliu.utils.SettingsUtil
import com.quansu.heifengwuliu.vmodel.SettingsVModel
import com.ysnows.base.base.BRepository
import com.ysnows.base.ccretrofit.CCRetrofit
import com.ysnows.base.ext.message
import com.ysnows.base.route.IUpdate
import com.ysnows.base.utils.MMKVManager
import com.ysnows.base.utils.UiSwitch


class SettingsActivity : MBActivity<SettingsVModel, ActivitySettingsBinding>() {

    override fun listeners() {
        super.listeners()

        binding.cbIcGoogle.isChecked = SettingsUtil.pinGoogleIcon()
        binding.cbClip.isChecked = SettingsUtil.clipboardListener()
        binding.cbAutoAd.isChecked = SettingsUtil.autoAd()
        binding.cbMsg.isChecked = SettingsUtil.showMsg()
        binding.cbDoublePaste.isChecked = SettingsUtil.doubleClickPaste()
        binding.layAboutUs.onClick {
            single(AboutActivity::class.java)
        }
        binding.layCheckupdate.setOnClickListener { v: View? ->

            CCRetrofit.instance()
                    .create(IUpdate::class.java)
                    .checkUpgrade(false)
                    .subscribe()

        }
        binding.layBackup.setOnClickListener { v: View? ->
            MessageDialogBuilder(context())
                    .setTitle("备份")
                    .setMessage("确定要将现在所有的设置备份到云中吗？")
                    .addAction("取消") { dialog: QMUIDialog, index: Int -> dialog.dismiss() }
                    .addAction("是的") { dialog: QMUIDialog?, index: Int ->
                        toast(R.string.developing)

//                        val funcs = App.getConfigOrm().query(Func::class.java)
//                        val searchEngines = App.getConfigOrm().query(SearchEngine::class.java)
                    }
                    .show()
        }

        binding.layRestore.setOnClickListener { v: View? ->
            MessageDialogBuilder(context())
                    .setTitle("恢复备份")
                    .setMessage("确定要恢复云中的备份？")
                    .addAction("取消") { dialog: QMUIDialog, which: Int -> dialog.dismiss() }
                    .addAction("是的") { dialog: QMUIDialog?, which: Int ->
                        toast(R.string.developing)

//                        SettingsUtil.backUp()
                    }
                    .show()
        }


        binding.cbIcGoogle.setOnCheckedChangeListener { buttonView, isChecked ->
            SettingsUtil.pinGoogleIcon(isChecked)
//            RoundSearchWidget.setDefault(context())
        }



        binding.btnLogout.onClick {
            message(R.string.enture_logout, negativeEnable = true, positiveAction = { dialog, _ ->
                MMKVManager.instance().clearAll()
                dialog.dismiss()
                UiSwitch.single_new_task_with_clear_task(context(), PrivateCenterActivity::class.java)
            })
        }
    }

    override fun binding(): ActivitySettingsBinding {
        return ActivitySettingsBinding.inflate(layoutInflater)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100) {
//            if (PermissionUtils.checkFloatPermission(this)) {
//                ClipBoardListenService.start(getContext());
//                SettingsUtil.clipboard_listener(1);
//            }
        }
    }

//    override fun onResume() {
//        super.onResume()
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
//            if (PermissionUtils.checkFloatPermission(this)) {
//                ClipBoardListenService.start(context())
//            }
//        }
//    }

    override fun title(): String? {
        return "设置"
    }

    override val isRxbus: Boolean
        get() = true

    override fun vmClass(): Class<SettingsVModel> {
        return SettingsVModel::class.java
    }

    override fun createRepository(): BRepository {
        return UserInfoRepository()
    }

    @Subscribe(tags = [Tag(ConfigRx.LOGIN)])
    fun userInfo(from: String) {
        vm.firstReq()
    }


}