package com.lanyun.wanglaotou.ui.activity

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.*
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.res.painterResource
import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.insets.statusBarsHeight
import com.lanyun.common.baseDate.themeTypeState
import com.lanyun.wanglaotou.MainActivity
import com.lanyun.wanglaotou.R
import com.lanyun.wanglaotou.composable.MainCompose
import com.lanyun.wanglaotou.extension.toast
import com.lanyun.wanglaotou.public.BaseScreen
import com.lanyun.wanglaotou.public.LcePage
import com.lanyun.wanglaotou.ui.theme.CustomThemeManager
import com.lanyun.wanglaotou.ui.theme.red
import com.lanyun.wanglaotou.viewModel.WelcomeModel
import com.lanyun.wanglaotou.viewModel.WelcomeViewAction
import com.lanyun.wanglaotou.viewModel.WelcomeViewEvent
import com.lanyun.wanglaotou.widget.StatsBarUtil
import com.tbruyelle.rxpermissions.RxPermissions
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect

class WelcomeActivity : ComponentActivity() {

    private val welcomeModel : WelcomeModel = WelcomeModel()

    @OptIn(ExperimentalAnimationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //防止出现按Home键回到桌面时，再次点击重新进入该界面bug
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT !== 0) {
            finish()
            return
        }

        setContent {
            val viewStates = welcomeModel.viewStates
            val themeState = themeTypeState.value
            WindowCompat.setDecorFitsSystemWindows(window, false)
            CustomThemeManager.WangLaoTouTheme(type = themeState ) {
                // A surface container using the 'background' color from the theme
                //可以获取状态栏高度
                ProvideWindowInsets {
                    StatsBarUtil().StatsBarColor(false)

                    BaseScreen{
                        LaunchedEffect(Unit) {
                            welcomeModel.viewEvents.collect{
                               when(it){
                                   is WelcomeViewEvent.ToMain -> {
                                       toMain()
                                   }
                               }
                            }

                        }

                        LcePage(pageState = viewStates.pageState, onRetry = {
                            welcomeModel.dispatch(WelcomeViewAction.FetchData)
                        }){
                            Row(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .background(MaterialTheme.colors.primary)
                                ,
                                verticalAlignment = Alignment.CenterVertically,
                                horizontalArrangement = Arrangement.Center
                            ){
                                Image(painter = painterResource(id = R.mipmap.logo), contentDescription = "")
                            }
                        }


                    }

                }
            }
        }
    }

    private fun toMain() {
//        RxPermissions(this)
//            .request(Manifest.permission.READ_EXTERNAL_STORAGE)
//            .subscribe {
//                if (it) {
//                    startActivity(Intent(this, MainActivity::class.java))
//                    finish()
//                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
//                } else
//                    toast(R.string.toast_permission_denied)
//            }
        startActivity(Intent(this, MainActivity::class.java))
        finish()
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)

    }

}