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

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
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 androidx.core.content.FileProvider
import com.alibaba.android.arouter.launcher.ARouter
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.core.provider.IBuildConfigProvider
import com.gh.gamecenter.core.provider.IPackageUtilsProvider
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
import com.lightgame.utils.Utils
import java.io.File

class ComposeGameDownloadSettingActivity : ComposeBaseActivity() {

    @SuppressLint("UnusedMaterialScaffoldPaddingParameter")
    @Composable
    override fun Content() {
        var installAutoSwitch by remember {
            mutableStateOf(SPUtils.getBoolean(Constants.SP_AUTO_INSTALL, true))
        }
        var favoriteAutoSwitch by remember {
            mutableStateOf(SPUtils.getBoolean(Constants.SP_CONCERN_GAME, true))
        }
        var trafficDownloadSwitch by remember {
            mutableStateOf(SPUtils.getBoolean(getTrafficDownloadHintKey(), false))
        }

        Scaffold(
            topBar = {
                SettingTopAppBar(getString(R.string.setting_game_download)) {
                    onBackPressed()
                }
            }, modifier = Modifier.fillMaxSize(), backgroundColor = HaloTheme.colors.background
        ) {
            Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
                Space(8.dp)
                SettingItem(
                    getString(R.string.setting_install_auto), showSwitch = true, switchStatus = installAutoSwitch
                ) {
                    SPUtils.setBoolean(Constants.SP_AUTO_INSTALL, !installAutoSwitch)
                    installAutoSwitch = !installAutoSwitch
                }
                SettingDivider()
                SettingItem(
                    getString(R.string.setting_favorite_auto), showSwitch = true, switchStatus = favoriteAutoSwitch
                ) {
                    SPUtils.setBoolean(Constants.SP_CONCERN_GAME, !favoriteAutoSwitch)
                    favoriteAutoSwitch = !favoriteAutoSwitch
                }
                SettingDivider()
                SettingItem(
                    getString(R.string.setting_traffic_download),
                    showSwitch = true,
                    switchStatus = trafficDownloadSwitch
                ) {
                    SPUtils.setBoolean(getTrafficDownloadHintKey(), !trafficDownloadSwitch)
                    trafficDownloadSwitch = !trafficDownloadSwitch
                }
                Space(8.dp)
                SettingItem(
                    getString(R.string.setting_download_path), content = getString(R.string.setting_download_path_des)
                ) {
                    val dirPath = Environment.getExternalStorageDirectory().path + "/gh-files"
                    startFilePath(dirPath)
                }
                SettingDivider()
                SettingItem(getString(R.string.setting_pic_path), content = getString(R.string.setting_pic_path_des)) {
                    val dirPath = Environment.getExternalStorageDirectory().path + "/pictures/ghzhushou"
                    startFilePath(dirPath)
                }
                Space(8.dp)
                SettingItem(getString(R.string.setting_clean_package), showArrow = true) {
                    ARouter.getInstance().build(RouteConsts.activity.cleanApkActivity).navigation()
                }
            }
        }
    }

    private fun startFilePath(dirPath: String) {
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        try {
            val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                val buildConfig =
                    ARouter.getInstance().build(RouteConsts.provider.buildConfig).navigation() as? IBuildConfigProvider
                FileProvider.getUriForFile(this, buildConfig?.getApplicationId() ?: "", File(dirPath))
            } else {
                Uri.fromFile(File(dirPath))
            }
            intent.setDataAndType(uri, "file/*")
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(intent)
        } catch (e: Exception) {
            Utils.toast(this, "无法找到文件管理器")
            e.printStackTrace()
        }
    }

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

        @JvmStatic
        fun getTrafficDownloadHintKey(): String {
            val packageUtils =
                ARouter.getInstance().build(RouteConsts.provider.packageUtils).navigation() as? IPackageUtilsProvider
            return packageUtils?.getGhVersionName() + "traffic_download_hint"
        }
    }
}