package com.example.what_the_fantasy.ui.screens

import android.content.Context
import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.MailOutline
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.what_the_fantasy.R
import coil.compose.AsyncImage
import com.example.what_the_fantasy.data.services.IServices
import com.example.what_the_fantasy.ui.components.NavBar
import com.example.what_the_fantasy.ui.theme.colorBackground
import com.example.what_the_fantasy.ui.theme.gradienBox
import com.example.what_the_fantasy.ui.theme.iconText
import com.example.what_the_fantasy.ui.theme.likeIcon
import com.example.what_the_fantasy.ui.theme.whiteBackcgroundText

@Composable
fun QuotePage(
    quoteId : Int,
    service : IServices,
    index : Int,
    navAccueil: (Int) -> Unit,
    navFavorite:(Int) -> Unit,
    navQuiz: (Int) -> Unit,
    navProfil:(Int) -> Unit)
{
    var quote = service.getQuote(quoteId) ?: return
    val context = LocalContext.current
    NavBar(onProfile = true,
        index = index,
        navControllerFavorite = navFavorite,
        navControllerAccueil = navAccueil,
        navControllerProfil = navProfil,
        navControllerQuiz = navQuiz
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(colorBackground),
            contentAlignment = Alignment.Center
        ) {
            Column(modifier = Modifier
                .padding(15.dp)
                .drawBehind {
                    drawRoundRect(
                        gradienBox,
                        cornerRadius = CornerRadius(15.dp.toPx()),
                    )
                }
            ) {
                Row(modifier = Modifier.padding(15.dp)) {
                    ImageQuote(
                        imageUrl = quote.imgUrl
                    )
                    Column {

                        FunctionalIcon(
                            // --/!\-- a modifier --/!\--
                            // isFavorite = service.isFavorite(id)
                            // --------------------------
                            true,
                            id = quoteId,
                            context = context
                        )
                        QuoteText(
                            text = '"' + quote.content + '"'
                        )
                    }
                }
                Column(modifier = Modifier
                    .padding(15.dp)
                    .fillMaxWidth()
                ) {
                    InfoQuoteText(
                        nameId = R.string.source,
                        text = quote.source
                    )
                    InfoQuoteText(
                        nameId = R.string.charac,
                        text = quote.character
                    )
                    Row(
                        modifier = Modifier
                            .padding(top = 10.dp)
                            .align(alignment = Alignment.End)
                    ) {
                        LikeInfo(
                            likes = quote.likes
                        )
                    }
                }
            }
        }
    }
}

// ------------------ Composants non partager de la page --------------------------------------

@Composable
fun QuoteText(text: String ){
    Text(
        text = text,
        modifier = Modifier.padding(start = 10.dp, top = 15.dp),
        fontWeight = FontWeight(1000),
        fontSize = 20.sp,
        color = iconText
    )
}

@Composable
fun ImageQuote(imageUrl : String){
    AsyncImage(
        model = imageUrl,
        contentDescription = stringResource(R.string.profilePict),
        modifier = Modifier
        .size(150.dp)
        .clip(RoundedCornerShape(15.dp))
    )
}

@Composable
fun FunctionalIcon(isFavorite: Boolean, id : Int, context : Context){
    Row(modifier = Modifier
        .fillMaxWidth()
    ) {
        IconButton(
            onClick = {

                val sendIntent = Intent().apply {
                    action = Intent.ACTION_SEND
                    // lien a changer quand le site sra deployer
                    putExtra(Intent.EXTRA_TEXT, "http://wfWebsite/quote/" + id.toString() )
                    type = "text/plain"
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // Ajout pour compatibilité
                }

                val shareIntent = Intent.createChooser(sendIntent, null)
                shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // Ajout aussi ici

                context.startActivity(shareIntent)
            },
            modifier = Modifier.padding(start = 15.dp)
        ) {
            Icon(
                Icons.Default.Share,
                contentDescription = stringResource(R.string.share),
                tint = iconText,
            )
        }
        IconButton(
            onClick = { }, //Go to comment
            modifier = Modifier.padding(start = 20.dp)
        ){
            Icon(
                Icons.Default.MailOutline,
                contentDescription = stringResource(R.string.comment),
                tint = iconText,
            )
        }

        if(isFavorite){
            IconButton(
                onClick = { }, //Go to comment
                modifier = Modifier.padding(start = 20.dp)
            ){
                Icon(
                    Icons.Default.Favorite,
                    contentDescription = stringResource(R.string.favorite),
                    tint = likeIcon,
                )
            }

        }
        else{
            IconButton(
                onClick = { }, //Go to comment
                modifier = Modifier.padding(start = 50.dp)
            ){
                Icon(
                    Icons.Default.FavoriteBorder,
                    contentDescription = stringResource(R.string.favorite),
                    tint = iconText
                )
            }
        }
    }
}

@Composable
fun InfoQuoteText(nameId : Int, text : String){
    Column(modifier = Modifier.padding(bottom = 20.dp)){
        Text(
            text = stringResource(id = nameId),
            fontSize = 18.sp,
            fontWeight = FontWeight(500),
            color = iconText
        )

        Text(
            text = text,
            color = whiteBackcgroundText,
            fontSize = 16.sp,
            fontWeight = FontWeight(400),
            modifier = Modifier
                .drawBehind {
                    drawRoundRect(
                        Color(255,255,255),
                        cornerRadius = CornerRadius(15.dp.toPx())
                    )
                }
                .padding(5.dp),
        )
    }
}

@Composable
fun LikeInfo(likes : Int){
        Text(
            text = likes.toString(),
            color = iconText
        )
        Icon(
            Icons.Default.Favorite,
            contentDescription = stringResource(R.string.favorite),
            tint = iconText,
        )
}