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

import androidx.compose.foundation.layout.*
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.RadioButton
import androidx.compose.material3.Button
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.applovin.sdk.AppLovinSdk
import com.bumptech.glide.Glide
import com.bytedance.msdk.api.TTMediationAdSdk
import com.bytedance.mtesttools.api.TTMediationTestTool
import com.composition.android.lib.ad.basic.Advertisers
import com.convenient.android.common.extension.readyGo
import com.convenient.android.lib.BuildConfig
import com.convenient.android.lib.ui.sample.ad.activity.*
import com.convenient.android.lib.ui.sample.ad.model.Datas
import com.convenient.android.lib.ui.sample.ad.viewmodel.AdConfigViewModel

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/6
 * description:
 */


@Composable
fun AdHomePage(viewModel: AdConfigViewModel, navHostController: NavHostController) {

    val list = Datas.getAdvertisers().toMutableList().apply {
        if (!BuildConfig.FLAVOR.equals("appLovinMaxTest")) {
            remove(Advertisers.AppLovinMax)
        }
    }

    val registerAdvertisers = remember {
        viewModel.advertisers
    }
    val expanded = remember {
        mutableStateOf(false)
    }

    val context = LocalContext.current

    Column {

        Box {
            Button(
                shape = RectangleShape,
                onClick = {
                    expanded.value = true
                }, modifier = Modifier.padding(bottom = 4.dp)
            ) {
                Text(text = "启用的广告商${if (registerAdvertisers.value == Advertisers.UNKNOWN) "" else ":${registerAdvertisers.value.name}"}")
            }

            DropdownMenu(expanded = expanded.value, offset = DpOffset(0.dp, 0.dp), onDismissRequest = { expanded.value = false }) {
                for (advertisers in list) {
                    DropdownMenuItem(onClick = {
                        viewModel.changeRegisterAdvertisers(advertisers)
                        expanded.value = false
                    }) {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            RadioButton(selected = registerAdvertisers.value == advertisers, onClick = null)
                            Text(text = advertisers.name, modifier = Modifier.padding(start = 8.dp))
                        }
                    }
                }
            }
        }

        if (registerAdvertisers.value != Advertisers.UNKNOWN) {

            val modifier = Modifier.fillMaxWidth()
            Item(modifier = modifier, title = "横幅广告") {
                context.readyGo(AdBannerActivity::class.java)
            }
            Item(modifier = modifier, title = "原生广告") {
                context.readyGo(AdNativeActivity::class.java)
            }
            Item(modifier = modifier, title = "原生模板广告") {
                context.readyGo(AdNativeTemplateActivity::class.java)
            }
            Item(modifier = modifier, title = "插屏广告") {
                context.readyGo(AdFullScreenAdActivity::class.java) {
                    it.putExtra("ad_slot_name", Datas.INTERSTITIAL)
                }
            }
            Item(modifier = modifier, title = "激励视频广告") {
                context.readyGo(AdFullScreenAdActivity::class.java) {
                    it.putExtra("ad_slot_name", Datas.REWARDED_VIDEO)
                }
            }
            Item(modifier = modifier, title = "激励插屏广告") {
                context.readyGo(AdFullScreenAdActivity::class.java) {
                    it.putExtra("ad_slot_name", Datas.REWARDED_INTERSTITIAL)
                }
            }
            Item(modifier = modifier, title = "开屏广告") {
                context.readyGo(AdAppOpenActivity::class.java) {
                }
            }

            if (registerAdvertisers.value == Advertisers.AppLovinMax) {
                Item(modifier = modifier, title = "AppLovinMax 中介测试套件") {
                    AppLovinSdk.getInstance(context).showMediationDebugger()
                }
            }

            if (registerAdvertisers.value == Advertisers.GroMore ||registerAdvertisers.value == Advertisers.CSJ) {
                Item(modifier = modifier, title = "AppLovinMax 中介测试套件") {
//                    TTMediationAdSdk.
                    TTMediationTestTool.launchTestTools(context) { p0, p1 ->
                        Glide.with(context)
                            .load(p1)
                            .into(
                                p0
                            )
                    }

                }
            }
        }
    }
}

@Composable
private fun Item(modifier: Modifier, title: String, onClick: () -> Unit) {
    FilledTonalButton(
        modifier = modifier,
        onClick = onClick
    ) {
        Text(text = title)
    }
}

