package com.convenient.android.lib

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.List
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.compose.rememberNavController
import com.convenient.android.common.extension.readyGo
import com.convenient.android.lib.ui.sample.ad.AdMainActivity
import com.convenient.android.lib.ui.sample.media.MediaSampleActivity
import com.convenient.android.lib.ui.sample.read.ReaderActivity
import com.convenient.android.lib.ui.sample.read.annotation.BottomItem
import com.convenient.android.lib.ui.theme.SampleTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            SampleTheme {
                MainPage()
            }
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun MainPagePreview() {
    SampleTheme {
        MainPage()
    }
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MainPage() {
    var bottomSelectedState by remember { mutableStateOf(0) }
    Column {
        SmallTopAppBar(title = {
            Text(text = "组件库", style = MaterialTheme.typography.titleMedium)
        }, colors = TopAppBarDefaults.smallTopAppBarColors(
            containerColor = MaterialTheme.colorScheme.primary.copy(alpha = .1F)
        ))
        Scaffold(bottomBar = {
            BottomBarView(selectedPosition = bottomSelectedState, onItemSelected = {
                bottomSelectedState = it
            })
        }) {
            when (bottomSelectedState) {
                0 -> FuncListView()
                1 -> ReaderView()
                else -> {
                    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                        Text(text = "建设中")
                    }
                }
            }
        }
    }
}

data class FuncBean(
    val title: String,
    val class_: Class<*>,
)

@Composable
private fun FuncListView() {
    val context = LocalContext.current
    val funcList = arrayListOf(
        FuncBean("DateUtils", DateUtilActivity::class.java),
        FuncBean("MediaUtils", MediaSampleActivity::class.java),
        FuncBean("广告测试", AdMainActivity::class.java)
    )
    Column {
        LazyColumn {
            items(funcList.size) { index ->
                ElevatedButton(modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp), onClick = {
                    context.readyGo(funcList[index].class_)
                }) {
                    Text(text = funcList[index].title)
                }
            }
        }
    }
}

@Composable
private fun ReaderView() {
    val context = LocalContext.current

    val navController = rememberNavController()

    val funcList = arrayListOf(
        FuncBean("阅读界面", ReaderActivity::class.java),
    )

    Column {
        LazyColumn {
            items(funcList.size) { index ->
                ElevatedButton(modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp), onClick = {
                    context.readyGo(funcList[index].class_)
                }) {
                    Text(text = funcList[index].title)
                }
            }
        }
    }
}

@Composable
private fun BottomBarView(selectedPosition: Int, onItemSelected: (position: Int) -> Unit) {
    val itemList = arrayListOf(
        BottomItem(title = "工具", Icons.Filled.Home),
        BottomItem(title = "PDF", Icons.Filled.List),
        BottomItem(title = "建设中", Icons.Filled.Build),
        BottomItem(title = "建设中", Icons.Filled.Build)
    )
    NavigationBar() {
        itemList.forEachIndexed { index, bottomItem ->
            NavigationBarItem(
                selected = selectedPosition == index,
                onClick = { onItemSelected.invoke(index) },
                icon = {
                    Icon(bottomItem.icon, contentDescription = null)
                },
                label = {
                    Text(bottomItem.title)
                }
            )
        }
    }
}
