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.Scaffold
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.setting.compose.R
import com.gh.gamecenter.setting.compose.base.ComposeBaseActivity
import com.gh.gamecenter.setting.compose.ui.component.*
import com.gh.gamecenter.setting.compose.ui.theme.HaloTheme

class ComposeVideoSettingActivity : ComposeBaseActivity() {

    @SuppressLint("UnusedMaterialScaffoldPaddingParameter")
    @Composable
    override fun Content() {
        val mContentVideoOptionSelected =
            SPUtils.getString(Constants.SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI) ?: Constants.VIDEO_OPTION_WIFI
        val mHomeOrDetailVideoOptionSelected =
            SPUtils.getString(Constants.SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI) ?: Constants.VIDEO_OPTION_WIFI

        var contentVideoOptionAll by remember {
            mutableStateOf(mContentVideoOptionSelected == Constants.VIDEO_OPTION_ALL)
        }
        var contentVideoOptionWifi by remember {
            mutableStateOf(mContentVideoOptionSelected == Constants.VIDEO_OPTION_WIFI)
        }
        var contentVideoOptionClose by remember {
            mutableStateOf(mContentVideoOptionSelected == Constants.VIDEO_OPTION_CLOSE)
        }
        var homeOrDetailVideoOptionAll by remember {
            mutableStateOf(mHomeOrDetailVideoOptionSelected == Constants.VIDEO_OPTION_ALL)
        }
        var homeOrDetailVideoOptionWifi by remember {
            mutableStateOf(mHomeOrDetailVideoOptionSelected == Constants.VIDEO_OPTION_WIFI)
        }
        var homeOrDetailVideoOptionClose by remember {
            mutableStateOf(mHomeOrDetailVideoOptionSelected == Constants.VIDEO_OPTION_CLOSE)
        }
        var muteSwitch by remember {
            mutableStateOf(SPUtils.getBoolean(Constants.SP_VIDEO_PLAY_MUTE, true))
        }
        Scaffold(
            topBar = {
                SettingTopAppBar(getString(R.string.setting_video)) {
                    onBackPressed()
                }
            }, modifier = Modifier.fillMaxSize(),
            backgroundColor = HaloTheme.colors.background
        ) {
            Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
                SettingTitle("内容视频")
                SettingItem(
                    getString(R.string.all_network_auto_play),
                    showSelected = true,
                    selectedStatus = contentVideoOptionAll
                ) {
                    if (!contentVideoOptionAll) {
                        contentVideoOptionAll = true
                        contentVideoOptionWifi = false
                        contentVideoOptionClose = false
                        SPUtils.setString(Constants.SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_ALL)
                    }
                }
                SettingDivider()
                SettingItem(
                    getString(R.string.only_wifi_auto_play),
                    showSelected = true,
                    selectedStatus = contentVideoOptionWifi
                ) {
                    if (!contentVideoOptionWifi) {
                        contentVideoOptionAll = false
                        contentVideoOptionWifi = true
                        contentVideoOptionClose = false
                        SPUtils.setString(Constants.SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI)
                    }
                }
                SettingDivider()
                SettingItem(
                    getString(R.string.close_auto_play),
                    showSelected = true,
                    selectedStatus = contentVideoOptionClose
                ) {
                    if (!contentVideoOptionClose) {
                        contentVideoOptionAll = false
                        contentVideoOptionWifi = false
                        contentVideoOptionClose = true
                        SPUtils.setString(Constants.SP_CONTENT_VIDEO_OPTION, Constants.VIDEO_OPTION_CLOSE)
                    }
                }
                SettingTitle("首页/游戏详情页视频")
                SettingItem(
                    getString(R.string.all_network_auto_play),
                    showSelected = true,
                    selectedStatus = homeOrDetailVideoOptionAll
                ) {
                    if (!homeOrDetailVideoOptionAll) {
                        homeOrDetailVideoOptionAll = true
                        homeOrDetailVideoOptionWifi = false
                        homeOrDetailVideoOptionClose = false
                        SPUtils.setString(Constants.SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_ALL)
                    }
                }
                SettingDivider()
                SettingItem(
                    getString(R.string.only_wifi_auto_play),
                    showSelected = true,
                    selectedStatus = homeOrDetailVideoOptionWifi
                ) {
                    if (!homeOrDetailVideoOptionWifi) {
                        homeOrDetailVideoOptionAll = false
                        homeOrDetailVideoOptionWifi = true
                        homeOrDetailVideoOptionClose = false
                        SPUtils.setString(Constants.SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_WIFI)
                    }
                }
                SettingDivider()
                SettingItem(
                    getString(R.string.close_auto_play),
                    showSelected = true,
                    selectedStatus = homeOrDetailVideoOptionClose
                ) {
                    if (!homeOrDetailVideoOptionClose) {
                        homeOrDetailVideoOptionAll = false
                        homeOrDetailVideoOptionWifi = false
                        homeOrDetailVideoOptionClose = true
                        SPUtils.setString(Constants.SP_HOME_OR_DETAIL_VIDEO_OPTION, Constants.VIDEO_OPTION_CLOSE)
                    }
                }
                Space(8.dp)
                SettingItem(
                    getString(R.string.setting_mute),
                    tips = getString(R.string.setting_mute_hint),
                    showSwitch = true,
                    switchStatus = muteSwitch
                ) {
                    SPUtils.setBoolean(Constants.SP_VIDEO_PLAY_MUTE, !muteSwitch)
                    muteSwitch = !muteSwitch
                }
            }
        }
    }

    companion object {
        fun getIntent(context: Context, entrance: String): Intent {
            return Intent(context, ComposeVideoSettingActivity::class.java).apply {
                putExtra(EntranceConsts.KEY_ENTRANCE, entrance)
            }
        }
    }
}