package com.gh.gamecenter.setting.compose.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Icon
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.common.utils.EnvHelper
import com.gh.gamecenter.core.provider.IDirectProvider
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.setting.compose.R
import com.gh.gamecenter.setting.compose.base.ComposeBaseActivity
import com.gh.gamecenter.setting.compose.clickableWithoutRipple
import com.gh.gamecenter.setting.compose.ui.component.SettingDivider
import com.gh.gamecenter.setting.compose.ui.component.SettingItem
import com.gh.gamecenter.setting.compose.ui.component.SettingTopAppBar
import com.gh.gamecenter.setting.compose.ui.component.Space
import com.gh.gamecenter.setting.compose.ui.theme.HaloTheme
import com.therouter.TheRouter
import com.therouter.router.Route
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(
    path = RouteConsts.activity.securityActivity,
    description = "帐号安全"
)
class ComposeSecurityActivity : ComposeBaseActivity() {

    private var mBindPhoneText by mutableStateOf("")

    @SuppressLint("UnusedMaterialScaffoldPaddingParameter")
    @Composable
    override fun Content() {
        Scaffold(
            topBar = {
                SettingTopAppBar(getString(R.string.setting_security)) {
                    onBackPressed()
                }
            }, modifier = Modifier.fillMaxSize(),
            backgroundColor = HaloTheme.colors.background
        ) {
            Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
                if (!intent.getBooleanExtra(EntranceConsts.KEY_DISPLAY_TYPE, false)) {
                    val registerType = when (UserManager.getInstance().userInfoEntity?.registerType) {
                        "qq" -> "QQ"
                        "wechat" -> "微信"
                        "weibo" -> "微博"
                        "douyin" -> "抖音"
                        else -> "手机"
                    }
                    UserManager.getInstance().userInfoEntity?.run {
                        mBindPhoneText = if (loginMobile.isNullOrEmpty()) "立即绑定" else loginMobile!!
                    }

                    Space(8.dp)
                    SettingItem(getString(R.string.register_way_key), content = registerType)
                    SettingDivider()
                    SettingItem(getString(R.string.bind_phone_key), showArrow = true, content = mBindPhoneText) {
                        startActivityForResult(
                            ComposeBindPhoneActivity.getNormalIntent(
                                this@ComposeSecurityActivity,
                                !UserManager.getInstance().userInfoEntity?.loginMobile.isNullOrEmpty()
                            ), INSERT_MOBILE_CODE
                        )
                    }
                    Space(8.dp)
                    SettingItem(getString(R.string.account_security_center), showArrow = true) {
                        startActivity(
                            getIntent(
                                this@ComposeSecurityActivity,
                                mEntrance,
                                true
                            )
                        )
                    }
                    Space(24.dp)
                    ContactService()
                } else {
                    Space(8.dp)
                    SettingItem("注销帐号", showArrow = true) {
                        val directUtils = TheRouter.get(IDirectProvider::class.java)
                        if (EnvHelper.isDevEnv) {
                            directUtils?.directToWebView(
                                this@ComposeSecurityActivity,
                                Constants.LOGOUT_ADDRESS_DEV,
                                mEntrance
                            )
                        } else {
                            directUtils?.directToWebView(
                                this@ComposeSecurityActivity,
                                Constants.LOGOUT_ADDRESS,
                                mEntrance
                            )
                        }
                    }
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == INSERT_MOBILE_CODE) {
            mBindPhoneText =
                if (UserManager.getInstance().userInfoEntity?.loginMobile.isNullOrEmpty()) {
                    "立即绑定"
                } else {
                    UserManager.getInstance().userInfoEntity?.loginMobile ?: ""
                }
        }
    }

    @Composable
    fun ContactService() {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                bitmap = ImageBitmap.imageResource(R.drawable.icon_doubt_gray),
                contentDescription = null,
                tint = Color.Unspecified,
                modifier = Modifier.size(12.dp)
            )
            Text(
                text = getString(R.string.security_tips),
                fontSize = 12.sp,
                color = HaloTheme.colors.textSubtitle,
                modifier = Modifier.padding(start = 2.dp)
            )
            Text(
                text = getString(R.string.security_contact),
                fontSize = 12.sp,
                color = HaloTheme.colors.themeFont,
                modifier = Modifier
                    .padding(start = 4.dp)
                    .clickableWithoutRipple {
                        val directUtils = TheRouter.get(IDirectProvider::class.java)
                        directUtils?.directToQqConversation(this@ComposeSecurityActivity, "800177318")
                    })
        }
    }

    companion object {
        const val INSERT_MOBILE_CODE = 411

        fun getIntent(context: Context, entrance: String, isLogoutStyle: Boolean = false): Intent {
            return Intent(context, ComposeSecurityActivity::class.java).apply {
                putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
                putExtra(EntranceConsts.KEY_DISPLAY_TYPE, isLogoutStyle)
            }
        }
    }

    // 登录事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(reuse: EBReuse) {
        if (reuse.type == Constants.LOGOUT_TAG) {
            finish()
        }
    }
}