package com.duben.microtribe.ui.activitys

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.duben.microtribe.R
import com.duben.microtribe.common.Constant
import com.duben.microtribe.manager.UserManager
import com.duben.microtribe.ui.activitys.base.BaseActivity
import com.duben.microtribe.ui.widgets.CustomDialogAsApple
import com.duben.microtribe.ui.widgets.DialogListener
import com.duben.microtribe.ui.widgets.DialogUtils
import com.duben.microtribe.utils.*
import com.duben.library.utils.nodoubleclick.AntiShake
import com.duben.microtribe.common.AppConfig
import kotlinx.android.synthetic.main.activity_settings.*
import kotlinx.android.synthetic.main.header_layout.*
import java.util.*

class SettingsActivity : BaseActivity(), View.OnClickListener {

    private val userManager by lazy { UserManager.getInstance() }

    override fun initViewsAndEvents() {
        initLayout()

    }

    override fun onResume() {
        super.onResume()

//        if (userManager.vipFlag) {
//            item_back.visibility = View.VISIBLE
//        } else {
//            item_back.visibility = View.GONE
//        }

        if (userManager.userIsLogin()) {
            btn_switch.visibility = View.VISIBLE
        } else {
            btn_switch.visibility = View.GONE
        }


    }

    @SuppressLint("CutPasteId")
    private fun initLayout() {
        tv_title.text = "设置"
        iv_left_icon.visibility = View.VISIBLE
        iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
        iv_left_icon.setOnClickListener(this)

        item_cleanCache.setOnClickListener(this)
        item_userAgree.setOnClickListener(this)
        item_privacyAgree.setOnClickListener(this)
        item_aboutUs.setOnClickListener(this)
        item_cancel.setOnClickListener(this)
        btn_switch.setOnClickListener(this)
        item_back.setOnClickListener(this)
        item_phone.setOnClickListener(this)

        item_invitedCode.findViewById<TextView>(R.id.tv_title).text = "微剧部落ID"
        val invitedCode = ContextCompat.getDrawable(this, R.mipmap.icon_settings_invite)
        invitedCode?.setBounds(0, 0, 56, 56)
        item_invitedCode.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(invitedCode, null, null, null)
        item_invitedCode.findViewById<TextView>(R.id.tv_right).visibility = View.VISIBLE
        item_invitedCode.findViewById<ImageView>(R.id.iv_right).visibility = View.GONE
        item_invitedCode.findViewById<TextView>(R.id.tv_right).text =
            userManager.codeId

        item_phone.findViewById<TextView>(R.id.tv_title).text = "手机号"
        val phone = ContextCompat.getDrawable(this, R.mipmap.icon_settings_tel)
        phone?.setBounds(0, 0, 56, 56)
        item_phone.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(phone, null, null, null)
        item_phone.findViewById<TextView>(R.id.tv_right).visibility = View.VISIBLE
        item_phone.findViewById<ImageView>(R.id.iv_right).visibility = View.GONE
        if (!TextUtils.isEmpty(userManager.mobile)) {
            item_phone.findViewById<TextView>(R.id.tv_right).text = userManager.mobile
        } else {
            item_phone.findViewById<TextView>(R.id.tv_right).text = "未绑定"
        }

        item_cleanCache.findViewById<TextView>(R.id.tv_title).text = "清理缓存"
        val cleanCache = ContextCompat.getDrawable(this, R.mipmap.icon_settings_clean)
        cleanCache?.setBounds(0, 0, 56, 56)
        item_cleanCache.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(cleanCache, null, null, null)
        item_cleanCache.findViewById<TextView>(R.id.tv_right).visibility = View.VISIBLE
        item_cleanCache.findViewById<ImageView>(R.id.iv_right).visibility = View.GONE
        item_cleanCache.findViewById<TextView>(R.id.tv_right).text =
            CacheUtil.getTotalCacheSize(this)

        item_userAgree.findViewById<TextView>(R.id.tv_title).text = "服务协议"
        val userAgree = ContextCompat.getDrawable(this, R.mipmap.icon_settings_user)
        userAgree?.setBounds(0, 0, 56, 56)
        item_userAgree.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(userAgree, null, null, null)
        item_userAgree.findViewById<TextView>(R.id.tv_right).visibility = View.GONE
        item_userAgree.findViewById<ImageView>(R.id.iv_right).visibility = View.VISIBLE

        item_privacyAgree.findViewById<TextView>(R.id.tv_title).text = "隐私协议"
        val privacyAgree = ContextCompat.getDrawable(this, R.mipmap.icon_settings_privacy)
        privacyAgree?.setBounds(0, 0, 56, 56)
        item_privacyAgree.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(privacyAgree, null, null, null)
        item_privacyAgree.findViewById<TextView>(R.id.tv_right).visibility = View.GONE
        item_privacyAgree.findViewById<ImageView>(R.id.iv_right).visibility = View.VISIBLE

        item_aboutUs.findViewById<TextView>(R.id.tv_title).text = "关于我们"
        val aboutUs = ContextCompat.getDrawable(this, R.mipmap.icon_settings_about)
        aboutUs?.setBounds(0, 0, 56, 56)
        item_aboutUs.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(aboutUs, null, null, null)
        item_aboutUs.findViewById<TextView>(R.id.tv_right).visibility = View.GONE
        item_aboutUs.findViewById<ImageView>(R.id.iv_right).visibility = View.VISIBLE

        item_cancel.findViewById<TextView>(R.id.tv_title).text = "注销账号"
        val cancel = ContextCompat.getDrawable(this, R.mipmap.icon_settings_cancel)
        cancel?.setBounds(1, 1, 55, 55)
        item_cancel.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(cancel, null, null, null)
        item_cancel.findViewById<TextView>(R.id.tv_right).visibility = View.GONE
        item_cancel.findViewById<ImageView>(R.id.iv_right).visibility = View.VISIBLE

        item_back.findViewById<TextView>(R.id.tv_title).text = "申请退款"
        val back = ContextCompat.getDrawable(this, R.mipmap.icon_settings_about)
        back?.setBounds(0, 0, 56, 56)
        item_back.findViewById<TextView>(R.id.tv_title)
            .setCompoundDrawables(back, null, null, null)
        item_back.findViewById<TextView>(R.id.tv_right).visibility = View.GONE
        item_back.findViewById<ImageView>(R.id.iv_right).visibility = View.VISIBLE
    }

    override fun getContentViewLayoutID() = R.layout.activity_settings

    override fun isApplyKitKatTranslucency() = false

    override fun onClick(v: View?) {
        if (AntiShake.check(v?.id)) return

        when (v?.id) {
            R.id.item_cleanCache -> {
                clearDialog()
            }
            R.id.item_userAgree -> {
                val bundle = Bundle()
                bundle.putString(WebActivity.WEB_TITLE, getString(R.string.register_name))
                bundle.putString(WebActivity.WEB_URL, Constant.REGISTER_URL)
                readyGo(WebActivity::class.java, bundle)
            }
            R.id.item_privacyAgree -> {
                val bundle = Bundle()
                bundle.putString(WebActivity.WEB_TITLE, getString(R.string.privacy_name))
                bundle.putString(WebActivity.WEB_URL, Constant.PRIVACY_URL)
                readyGo(WebActivity::class.java, bundle)
            }
            R.id.item_aboutUs -> {
                readyGo(AboutusActivity::class.java)
            }
            R.id.btn_switch -> {
                backDialog()
            }
            R.id.iv_left_icon -> {
                onBackPressed()
            }
            R.id.item_cancel -> {
                cancelAccount()
            }
            R.id.item_phone -> {
                if (TextUtils.isEmpty(userManager.mobile)) {
                    readyGo(MobileLoginActivity::class.java)
                    return
                }
            }
            R.id.item_back -> {
                readyGo(Backcash2Activity::class.java)
            }
        }
    }

    private fun cancelAccount() {
        DialogUtils.showDialog(
            this,
            SpanUtils()
                .append("确认注销账户？")
                .setForegroundColor(ContextCompat.getColor(this, R.color.tv_message_recente_money))
                .create(),
            "提示",
            "确定",
            object : DialogListener() {
                override fun onClick(dialog: Dialog?, v: View?) {
                    dialog?.dismiss()

                    ToastUtil.show(this@SettingsActivity, "注销账户申请已发起!")

                    Handler(Looper.getMainLooper()).postDelayed({
                        finish()
                    }, 200)

//                    UcropUtils.clipData(address)
//                    ToastUtil.show(this@SettingsActivity, "邮箱已经复制到您的剪切板上!")
//
//                    val uri = Uri.parse("mailto:$address")
//                    val data = Intent(Intent.ACTION_SENDTO, uri)
//                    data.putExtra(Intent.EXTRA_EMAIL, address)
//                    data.putExtra(Intent.EXTRA_SUBJECT, "申请注销账户")
//                    data.putExtra(Intent.EXTRA_TEXT, "申请注销账户ID：\n申请注销账户微信号：\n申请注销账户手机号：\n")
//                    startActivity(Intent.createChooser(data, "选择邮箱"))
                }
            })
    }

    private fun backDialog() {
        val cdaa = CustomDialogAsApple(context, object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                dialog?.dismiss()
                when (v?.id) {
                    R.id.dialog_btn_left -> {
                        AppConfig.exitLoginMainRefresh = true
                        userManager.userLogout()
                        finish()

//                        // 修复 异常999问题
//                        for (activity in AppManager.getActivityStack()) {
//                            if (activity is MainActivity) {
//                                activity.clickTab1Layout()
//                                break
//                            }
//                        }
//                        Handler(Looper.getMainLooper()).postDelayed({
//                            Constant.isFirstOpenDialog = false
//                            readyGoThenKill(MainActivity::class.java)
//                        }, 200)
                    }
                }
            }
        })
        cdaa.setContent("确定要退出登录吗？")
        cdaa.setLeft("确定")
        cdaa.setRight("取消")
        cdaa.show()
    }

    private fun clearDialog() {
        val cdaa = CustomDialogAsApple(context, object : DialogListener() {
            override fun onClick(dialog: Dialog?, v: View?) {
                dialog?.dismiss()
                when (v?.id) {
                    R.id.dialog_btn_left -> {

                    }
                    R.id.dialog_btn_right -> {
                        CacheUtil.clearAllCache()
                        item_cleanCache.findViewById<TextView>(R.id.tv_right).text = "0KB"
                    }
                }
            }
        })
        cdaa.setTitle("提示")
        cdaa.setContent("确定清除缓存吗?")
        cdaa.setLeft("取消")
        cdaa.setRight("确定")
        cdaa.show()
    }
}