package com.convenient.android.lib.ui.sample.ad.activity

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import com.composition.android.lib.ad.AdLoad
import com.composition.android.lib.ad.basic.AdResult
import com.convenient.android.common.base.view.BaseActivity
import com.convenient.android.common.base.viewbinding.BaseBindingActivity
import com.convenient.android.common.extension.readyGo
import com.convenient.android.lib.R
import com.convenient.android.lib.databinding.ActivityAppWelcomeBinding
import com.convenient.android.lib.ui.sample.MainActivity
import com.convenient.android.lib.ui.sample.ad.model.Datas
import com.convenient.android.lib.ui.theme.SampleTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/9
 * description:
 */
class AppWelcomeActivity : BaseBindingActivity<ActivityAppWelcomeBinding>(ActivityAppWelcomeBinding::inflate) {

    companion object {
        const val ONLY_SHOW = "only_show"
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {

            val onlyShow = intent.getBooleanExtra(ONLY_SHOW, false)
            if (onlyShow) {
                showAd {
                    lifecycleScope.launch {
                        delay(300)
                        finish()
                    }
                }
            }else{
                showAd {
                    toGoMainActivity()
                }
            }
        }
    }

    private suspend fun showAd(result : () -> Unit){

        AdLoad.loadAd(this, Datas.APP_OPEN)
            .collect{
                if (it is AdResult.Success){
                    delay(200)
                    AdLoad.showSplash(this, binding.llAdContent, it){
                        onAdClose {
                            result.invoke()
                        }
                        onAdLoadedFail {
                            result.invoke()
                        }
                        onAdSkip {
                            result.invoke()
                        }
                        onTimeOver {
                            result.invoke()
                        }
                    }
                }else{
                    result.invoke()
                }
            }
    }


    private fun toGoMainActivity(delay : Long = 3000){

        lifecycleScope.launch {
            delay(delay)
            this@AppWelcomeActivity.readyGo(MainActivity::class.java)
            finish()
        }

    }
}

