package com.example.oscellamobile.screens

import android.icu.text.ListFormatter.Width
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
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.fillMaxWidth
import androidx.compose.foundation.layout.height
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.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
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.draw.shadow
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.oscellamobile.Auth
import com.example.oscellamobile.R
import org.w3c.dom.Text

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Auth(navController: NavHostController) {
    val viewModel = Auth()
    val kdamFontFamily = FontFamily(Font(R.font.kdam))
    val email = remember { mutableStateOf("") }
    val password = remember { mutableStateOf("") }

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

    val gradientButton1 = Brush.linearGradient(
        colors = listOf(
            Color(0xFF2EC4B6),
            Color(0xFFFF9F1C)
        ),
        start = Offset(0f, 0f),
        end = Offset(150f, 150f)
    )

    Canvas(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
    ) {
        val gradient = Brush.linearGradient(
            colors = listOf(
                Color(0xFF2EC4B6),
                Color(0xFFFF9F1C),
                Color(0xFFE71D36)
            ),
            start = Offset(size.width, 1200f),
            end = Offset(150f, size.height)
        )

        drawRect(brush = gradient)
    }

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(modifier = Modifier.height(180.dp))
        Text(
            text = "Oscella", fontSize = 40.sp,
            fontFamily = kdamFontFamily
        )
        Spacer(modifier = Modifier.height(29.dp))
        TextField(
            value = email.value,
            onValueChange = { newText -> email.value = newText },
            placeholder = { Text("Email", fontSize = 24.sp, fontFamily = kdamFontFamily) },
            shape = RoundedCornerShape(20.dp),
            colors = TextFieldDefaults.textFieldColors(
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent
            ),
            textStyle = TextStyle(
                fontSize = 20.sp,
                fontFamily = kdamFontFamily
            ),
            modifier = Modifier
                .width(330.dp)
                .height(66.dp)
        )
        Spacer(modifier = Modifier.height(20.dp))
        TextField(
            value = password.value,
            onValueChange = {newText -> password.value = newText},
            placeholder = { Text("Password", fontSize = 24.sp, fontFamily = kdamFontFamily) },
            shape = RoundedCornerShape(20.dp),
            colors = TextFieldDefaults.textFieldColors(
                focusedIndicatorColor = Color.Transparent,
                unfocusedIndicatorColor = Color.Transparent
            ),
            textStyle = TextStyle(
                fontSize = 20.sp,
                fontFamily = kdamFontFamily
            ),
            modifier = Modifier
                .width(330.dp)
                .height(66.dp)
        )
        Spacer(modifier = Modifier.height(20.dp))
        Button(
            onClick = {
                viewModel.onSignInEmailPassword(email.value, password.value, navController)
            },
            modifier = Modifier
                .shadow(100.dp)
                .width(180.dp)
                .height(50.dp)
                .background(brush = gradientButton, shape = RoundedCornerShape(20.dp)),
            colors = ButtonDefaults.buttonColors(
                containerColor = Color.Transparent
            )

        ) {
            Text("Login", color = Color.White, fontFamily = kdamFontFamily, fontSize = 24.sp)
        }
        Text(
            text = "Don’t have account?",
            color = Color.White,
            fontFamily = kdamFontFamily,
            fontSize = 14.sp
        )
        Spacer(modifier = Modifier.height(10.dp))
        Row(
                horizontalArrangement = Arrangement.Center,
        ) {
            Button(
                onClick = {navController.navigate("Registration")},
                modifier = Modifier
                    .width(100.dp)
                    .height(40.dp)
                    .background(brush = gradientButton1, shape = RoundedCornerShape(20.dp)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.Transparent
                )

            ) {
                Text("SignUp", color = Color.White, fontFamily = kdamFontFamily, fontSize = 14.sp)
            }
            Spacer(modifier = Modifier.width(16.dp))
            Button(
                onClick = { },
                modifier = Modifier
                    .width(100.dp)
                    .height(40.dp)
                    .background(brush = gradientButton1, shape = RoundedCornerShape(20.dp)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.Transparent
                )

            ) {
                Text("Guest", color = Color.White, fontFamily = kdamFontFamily, fontSize = 14.sp)
            }
        }
    }
}


