package com.iscs.bozzys.ui.pages.select.job

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.iscs.bozzys.R
import com.iscs.bozzys.ui.common.PageBase
import com.iscs.bozzys.ui.common.Title
import com.iscs.bozzys.ui.pages.compose.CardBox
import com.iscs.bozzys.ui.pages.create.job.openPageCreateJob
import com.iscs.bozzys.ui.theme.Main

fun Context.openPageSelectJobType() {
    startActivity(Intent(this, PageSelectJobType::class.java))
}

class PageSelectJobType : PageBase() {

    @OptIn(ExperimentalLayoutApi::class)
    @Composable
    override fun GetViews(pv: PaddingValues) {
        var keywords by remember { mutableStateOf("") }
        Column(
            Modifier
                .fillMaxSize()
                .background(Color(0xFFF8F9FA))
        ) {
            Title(pv, "新建作业")
            // 搜索框
            Row(
                Modifier
                    .background(brush = Brush.horizontalGradient(listOf(Color(0xFFFF8C00), Color(0xFFFFA500))))
                    .padding(horizontal = 16.dp, vertical = 8.dp)
            ) {
                BasicTextField(
                    keywords,
                    onValueChange = { keywords = it },
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(40.dp)
                        .clip(RoundedCornerShape(50))
                        .background(brush = Brush.horizontalGradient(listOf(Color(0xFFFFA126), Color(0xFFFFB126))))
                        .padding(horizontal = 16.dp),
                    singleLine = true,
                    textStyle = LocalTextStyle.current.copy(fontSize = 16.sp, lineHeight = 18.sp, color = Color.White),
                    decorationBox = { innerTextField ->
                        Box(contentAlignment = Alignment.CenterStart) {
                            innerTextField()
                            if (keywords.isEmpty()) {
                                val text = "搜索作业模板名称..."
                                Text(
                                    text,
                                    color = Color.White.copy(alpha = 0.8f),
                                    fontSize = 16.sp,
                                    lineHeight = 18.sp
                                )
                            }
                        }
                    },
                    cursorBrush = SolidColor(Color.White),
                )
            }
            // 底部显示内容
            LazyColumn(Modifier.fillMaxSize()) {
                item {
                    CardBox(Modifier.padding(16.dp)) {
                        // 确定数据后，这里的代码应该抽离为一个组件
                        Column(Modifier.fillMaxWidth()) {
                            Row(
                                Modifier
                                    .fillMaxWidth()
                                    .height(40.dp)
                                    .padding(horizontal = 16.dp), verticalAlignment = Alignment.CenterVertically
                            ) {
                                Icon(
                                    painter = painterResource(R.drawable.code),
                                    contentDescription = "",
                                    tint = MaterialTheme.colorScheme.primary,
                                    modifier = Modifier
                                        .padding(end = 8.dp)
                                        .size(14.dp)
                                )
                                Text("能量隔离（LOTO）", fontSize = 14.sp, fontWeight = FontWeight.Medium, color = MaterialTheme.colorScheme.primary)
                            }
                            Spacer(
                                Modifier
                                    .fillMaxWidth()
                                    .height(1.dp)
                                    .background(Color(0xFFF0F0F0))
                            )
                            val list = listOf(1, 2, 3, 4)
                            FlowRow(
                                modifier = Modifier.fillMaxWidth(),
                                maxItemsInEachRow = 3
                            ) {
                                list.forEachIndexed { idx, i ->
                                    Box(
                                        Modifier
                                            .fillMaxWidth((1 / 3f) - 0.0005f) // 这里是为了部分手机显示的问题导致只能显示俩个
                                            .aspectRatio(1f)
                                            .background(Color(0xFFF0F0F0))
                                            .padding(
                                                end = if ((idx + 1) % 3 == 0) 0.dp else 1.dp,
                                                bottom = if ((idx + 1) > ((list.size / 3) * 3)) 0.dp else 1.dp
                                            )
                                    ) {
                                        Column(
                                            Modifier
                                                .fillMaxSize()
                                                .background(Color.White)
                                                .clickable(onClick = { openPageCreateJob(0) }),
                                            horizontalAlignment = Alignment.CenterHorizontally,
                                            verticalArrangement = Arrangement.Center
                                        ) {
                                            Icon(
                                                painter = painterResource(R.drawable.location),
                                                contentDescription = "",
                                                modifier = Modifier
                                                    .fillMaxSize(1 / 3f)
                                                    .clip(RoundedCornerShape(12.dp))
                                                    .background(Main)
                                                    .padding(10.dp),
                                                tint = Color.White
                                            )
                                            Text("能量隔离$i", fontSize = 12.sp, modifier = Modifier.padding(top = 5.dp))
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}