package com.example.oscellamobile.screens

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
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.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
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.shadow
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RadialGradientShader
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.oscellamobile.R
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import kotlinx.coroutines.delay
import kotlin.math.cos
import kotlin.math.sin


@Composable
fun MainWindow(navController: NavHostController) {
    val systemUiController = rememberSystemUiController()

    // Automatically hide the status bar when the composable is first displayed
    LaunchedEffect(Unit) {
        systemUiController.isStatusBarVisible = false // Hide the status bar by default
        delay(5000) // Keep it visible for 5 seconds (if needed, you can adjust this)
        systemUiController.isStatusBarVisible = false // Ensure it stays hidden
    }

    val gradientButton = Brush.linearGradient(
        colors = listOf(
            Color(0xFFE71D36),
            Color(0xFFFF9F1C)
        ),
        start = Offset(30f, 30f),
        end = Offset(200f, 200f)
    )

    val kdamFontFamily = FontFamily(Font(R.font.kdam))
    val playfairdisplay = FontFamily(Font(R.font.playfair))
    val caveat = FontFamily(Font(R.font.caveat))
    Canvas(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
    ) {
        val gradient = Brush.linearGradient(
            colors = listOf(
                Color(0xFFFF9F1C),
                Color(0xFFE71D36)
            ),
            start = Offset(size.width, 1200f),
            end = Offset(150f, size.height)
        )

        drawRect(brush = gradient)
    }

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
    ) {
        Spacer(modifier = Modifier.height(10.dp))
        Row {
            Spacer(modifier = Modifier.width(13.dp))
            Text(text = "Osccela", fontSize = 40.sp, fontFamily = kdamFontFamily)
            Spacer(modifier = Modifier.width(145.dp))
            Image(
                painter = painterResource(id = R.drawable.personall),
                contentDescription = "Описание изображения",
                modifier = Modifier.size(90.dp)
            )
        }
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(70.dp)
                .padding(10.dp, 0.dp, 10.dp, 0.dp)
                .background(color = Color(0xFFFDFFFC), shape = RoundedCornerShape(5.dp)),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Spacer(Modifier.width(10.dp))
            Button(
                onClick = {},
                modifier = Modifier
                    .shadow(100.dp)
                    .width(93.dp)
                    .height(45.dp)
                    .background(brush = gradientButton, shape = RoundedCornerShape(5.dp)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.Transparent
                )
            ) {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    Text("Игры", color = Color(0xFFFDFFFC), fontFamily = playfairdisplay, fontSize = 17.sp)
                }
            }

            Button(
                onClick = {},
                modifier = Modifier
                    .shadow(100.dp)
                    .width(135.dp)
                    .height(45.dp)
                    .background(brush = gradientButton, shape = RoundedCornerShape(5.dp)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.Transparent
                )
            ) {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    Text("Компании", color = Color(0xFFFDFFFC), fontFamily = playfairdisplay, fontSize = 17.sp)
                }
            }

            Button(
                onClick = {},
                modifier = Modifier
                    .shadow(100.dp)
                    .width(107.dp)
                    .height(45.dp)
                    .background(brush = gradientButton, shape = RoundedCornerShape(5.dp)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.Transparent
                )
            ) {
                Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    Text("Облако", color = Color(0xFFFDFFFC), fontFamily = playfairdisplay, fontSize = 17.sp)
                }
            }
            Spacer(Modifier.width(10.dp))
        }
        Spacer(Modifier.height(100.dp))
        Card(
            modifier = Modifier
                .width(400.dp)
                .height(320.dp)
                .padding(20.dp, 0.dp, 20.dp, 0.dp)
                .background(color = Color(0xFFFDFFFC), shape = RoundedCornerShape(15.dp))
        ) {
            Row {
                Spacer(Modifier.width(10.dp))
                Column {
                    Text(
                        text = "Hoyoverse",
                        fontSize = 20.sp,
                        fontFamily = kdamFontFamily,
                        color = Color(0xFF2EC4B6)
                    )
                    Text(text = "Фэнтези", fontSize = 12.sp, fontFamily = kdamFontFamily, color = Color(0xFFE71D36))
                    Spacer(Modifier.width(20.dp))
                    Column {
                        Text(
                            text = "Основные метки:",
                            fontSize = 14.sp,
                            fontFamily = kdamFontFamily,
                            color = Color(0xFF7F807E)
                        )
                        Column(
                            Modifier.padding(10.dp)
                        ) {
                            Box(
                                modifier = Modifier
                                    .width(107.dp)
                                    .height(22.dp)
                                    .background(
                                        color = Color(0xFF2EC4B6),
                                        shape = RoundedCornerShape(20.dp)
                                    ),
                                contentAlignment = Alignment.Center
                            )
                            {
                                Text(
                                    text = "Пошаговая",
                                    color = Color(0xFFFDFFFC),
                                    fontFamily = caveat,
                                    fontSize = 20.sp
                                )
                            }
                            Spacer(Modifier.height(7.dp))
                            Box(
                                modifier = Modifier
                                    .width(107.dp)
                                    .height(22.dp)
                                    .background(
                                        color = Color(0xFF2EC4B6),
                                        shape = RoundedCornerShape(20.dp)
                                    ),
                                contentAlignment = Alignment.Center
                            )
                            {
                                Text(
                                    text = "Ролевая",
                                    color = Color(0xFFFDFFFC),
                                    fontFamily = caveat,
                                    fontSize = 20.sp
                                )
                            }
                            Spacer(Modifier.height(7.dp))
                            Box(
                                modifier = Modifier
                                    .width(107.dp)
                                    .height(22.dp)
                                    .background(
                                        color = Color(0xFF2EC4B6),
                                        shape = RoundedCornerShape(20.dp)
                                    ),
                                contentAlignment = Alignment.Center
                            )
                            {
                                Text(
                                    text = "Стратегия",
                                    color = Color(0xFFFDFFFC),
                                    fontFamily = caveat,
                                    fontSize = 20.sp
                                )
                            }
                        }
                    }
                }
                Column {
                    Row {
                        Spacer(Modifier.width(45.dp))
                        Box(
                            modifier = Modifier
                                .width(130.dp)
                                .height(30.dp)
                                .border(
                                    BorderStroke(2.dp, Color(0x80B0B0B0)),
                                    shape = RoundedCornerShape(15.dp)
                                )
                        )
                        {
                            Text(
                                text = "Избранное: 23",
                                fontSize = 16.sp,
                                fontFamily = playfairdisplay,
                                color = Color(0xFFFF9F1C),
                                modifier = Modifier.align(Alignment.Center)
                            )
                        }
                        val size = 31.dp

                        var isOrange by remember { mutableStateOf(false) }

                        val buttonColor = if (isOrange) Color(0xFFFF9F1C) else Color.Gray

                        Canvas(
                            modifier = Modifier
                                .size(30.dp)
                                .clickable(onClick = { isOrange = !isOrange })
                        ) {
                            drawStar(center = center, radius = size.toPx() / 2, color = buttonColor)
                        }
                    }
                    Row {
                        Spacer(Modifier.width(10.dp))
                        Text(
                            text = "Honkai Star Rail",
                            fontSize = 20.sp,
                            fontFamily = kdamFontFamily,
                            color = Color(0xFF7F807E),
                        )
                        Spacer(Modifier.width(10.dp))
                        Text(
                            text = "16+",
                            fontSize = 20.sp,
                            fontFamily = kdamFontFamily,
                            color = Color(0xFFE71D36),
                        )
                    }
                    Image(
                        painter = painterResource(id = R.drawable.test),
                        contentDescription = "Описание изображения",
                        modifier = Modifier.width(300.dp).height(138.dp)
                    )
                }
            }
        }
    }
}

fun DrawScope.drawStar(center: Offset, radius: Float, color: Color) {
    val path = androidx.compose.ui.graphics.Path()
    val points = 5
    val angle = 2 * Math.PI / points
    val innerRadius = radius / 2.5f

    for (i in 0 until points * 2) {
        val r = if (i % 2 == 0) radius else innerRadius
        val x = center.x + r * cos(i * angle - Math.PI / 2)
        val y = center.y + r * sin(i * angle - Math.PI / 2)

        if (i == 0) {
            path.moveTo(x.toFloat(), y.toFloat())
        } else {
            path.lineTo(x.toFloat(), y.toFloat())
        }
    }
    path.close()
    drawPath(path, color)
}