package id.iiyh.inatrading.feature.info.presentation

import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.background
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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.CheckCircle
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
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.rotate
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import id.iiyh.inatrading.R
import id.iiyh.inatrading.core.ui.components.InaInnerTopAppBar
import id.iiyh.inatrading.core.ui.components.InaPrimaryButton
import id.iiyh.inatrading.core.ui.theme.AccentBlue
import id.iiyh.inatrading.core.ui.theme.AccentPurple
import id.iiyh.inatrading.core.ui.theme.AccentPurpleContainer
import id.iiyh.inatrading.core.ui.theme.Background
import id.iiyh.inatrading.core.ui.theme.BrandRed
import id.iiyh.inatrading.core.ui.theme.BrandRedLight
import id.iiyh.inatrading.core.ui.theme.ManropeFontFamily
import id.iiyh.inatrading.core.ui.theme.OnSurface
import id.iiyh.inatrading.core.ui.theme.OnSurfaceVariant
import id.iiyh.inatrading.core.ui.theme.SurfaceContainerHighest
import id.iiyh.inatrading.core.ui.theme.SurfaceContainerLow
import id.iiyh.inatrading.core.ui.theme.SurfaceContainerLowest

// TODO: set to real WA support number e.g. "628123456789"
private const val WA_SUPPORT_NUMBER = ""

@Composable
fun TermsConditionsScreen(
    onBack: () -> Unit,
) {
    val context = LocalContext.current

    Scaffold(
        topBar = { InaInnerTopAppBar(onBack = onBack) },
        containerColor = Background,
    ) { innerPadding ->
        Box(modifier = Modifier.fillMaxSize()) {

            // Brand slope decorative background
            Box(
                modifier = Modifier
                    .offset(x = 80.dp, y = 160.dp)
                    .width(320.dp)
                    .height(600.dp)
                    .rotate(-2f)
                    .clip(RoundedCornerShape(48.dp))
                    .background(SurfaceContainerHighest.copy(alpha = 0.30f))
            )

            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(innerPadding)
                    .verticalScroll(rememberScrollState())
                    .padding(horizontal = 24.dp)
                    .padding(bottom = 40.dp),
                verticalArrangement = Arrangement.spacedBy(20.dp),
            ) {
                Spacer(modifier = Modifier.height(12.dp))

                // ── Hero ──────────────────────────────────────────────────────
                Box(
                    modifier = Modifier
                        .clip(RoundedCornerShape(percent = 50))
                        .background(AccentPurpleContainer.copy(alpha = 0.25f))
                        .padding(horizontal = 16.dp, vertical = 6.dp),
                ) {
                    Text(
                        text = stringResource(R.string.terms_label).uppercase(),
                        style = MaterialTheme.typography.labelSmall,
                        fontWeight = FontWeight.SemiBold,
                        color = AccentPurple,
                        letterSpacing = 1.5.sp,
                    )
                }

                Text(
                    text = buildAnnotatedString {
                        withStyle(SpanStyle(color = OnSurface)) {
                            append(stringResource(R.string.terms_headline_part1))
                            append("\n")
                        }
                        withStyle(SpanStyle(color = BrandRed)) {
                            append(stringResource(R.string.terms_headline_part2))
                        }
                    },
                    fontFamily = ManropeFontFamily,
                    fontWeight = FontWeight.ExtraBold,
                    fontSize = 42.sp,
                    lineHeight = 50.sp,
                )

                Text(
                    text = stringResource(R.string.terms_intro),
                    style = MaterialTheme.typography.bodyLarge,
                    color = OnSurfaceVariant,
                    lineHeight = 26.sp,
                )

                Spacer(modifier = Modifier.height(4.dp))

                // ── Section 01 ────────────────────────────────────────────────
                TermsArticle(
                    number = "01",
                    numberColor = BrandRed.copy(alpha = 0.20f),
                    title = stringResource(R.string.terms_section1_title),
                    accentColor = BrandRed,
                    content = {
                        Text(
                            text = stringResource(R.string.terms_section1_p1),
                            style = MaterialTheme.typography.bodyMedium,
                            color = OnSurfaceVariant,
                            lineHeight = 22.sp,
                        )
                        Spacer(modifier = Modifier.height(12.dp))
                        Text(
                            text = stringResource(R.string.terms_section1_p2),
                            style = MaterialTheme.typography.bodyMedium,
                            color = OnSurfaceVariant,
                            lineHeight = 22.sp,
                        )
                    },
                )

                // ── Section 02 ────────────────────────────────────────────────
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(12.dp))
                        .background(SurfaceContainerLow)
                        .padding(start = 4.dp),
                ) {
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .clip(RoundedCornerShape(topStart = 0.dp, topEnd = 12.dp, bottomStart = 0.dp, bottomEnd = 12.dp))
                            .background(SurfaceContainerLow)
                            .padding(24.dp),
                        verticalArrangement = Arrangement.spacedBy(12.dp),
                    ) {
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.spacedBy(12.dp),
                        ) {
                            Text(
                                text = "02",
                                fontFamily = ManropeFontFamily,
                                fontWeight = FontWeight.ExtraBold,
                                fontSize = 32.sp,
                                color = AccentBlue.copy(alpha = 0.20f),
                            )
                            Text(
                                text = stringResource(R.string.terms_section2_title),
                                fontFamily = ManropeFontFamily,
                                fontWeight = FontWeight.Bold,
                                fontSize = 20.sp,
                                color = OnSurface,
                            )
                        }

                        Text(
                            text = stringResource(R.string.terms_section2_p1),
                            style = MaterialTheme.typography.bodyMedium,
                            color = OnSurfaceVariant,
                            lineHeight = 22.sp,
                        )

                        // Checklist items
                        ChecklistItem(text = stringResource(R.string.terms_section2_check1))
                        ChecklistItem(text = stringResource(R.string.terms_section2_check2))

                        // Image placeholder
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(160.dp)
                                .clip(RoundedCornerShape(12.dp))
                                .background(SurfaceContainerHighest),
                            contentAlignment = Alignment.Center,
                        ) {
                            Text(
                                text = "[ Payment Image ]",
                                style = MaterialTheme.typography.bodySmall,
                                color = OnSurfaceVariant.copy(alpha = 0.4f),
                            )
                        }
                    }
                }

                // ── Section 03 ────────────────────────────────────────────────
                TermsArticle(
                    number = "03",
                    numberColor = AccentPurple.copy(alpha = 0.20f),
                    title = stringResource(R.string.terms_section3_title),
                    accentColor = AccentPurple,
                    content = {
                        Text(
                            text = stringResource(R.string.terms_section3_p1),
                            style = MaterialTheme.typography.bodyMedium,
                            color = OnSurfaceVariant,
                            lineHeight = 22.sp,
                        )
                        Spacer(modifier = Modifier.height(12.dp))
                        Text(
                            text = stringResource(R.string.terms_section3_p2),
                            style = MaterialTheme.typography.bodyMedium,
                            color = OnSurfaceVariant,
                            lineHeight = 22.sp,
                        )
                    },
                )

                // ── Contact CTA ───────────────────────────────────────────────
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(16.dp))
                        .background(SurfaceContainerHighest)
                        .padding(24.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(16.dp),
                ) {
                    Column(modifier = Modifier.weight(1f)) {
                        Text(
                            text = stringResource(R.string.terms_contact_title),
                            fontFamily = ManropeFontFamily,
                            fontWeight = FontWeight.Bold,
                            fontSize = 16.sp,
                            color = OnSurface,
                        )
                        Text(
                            text = stringResource(R.string.terms_contact_desc),
                            style = MaterialTheme.typography.bodySmall,
                            color = OnSurfaceVariant,
                        )
                    }
                    InaPrimaryButton(
                        text = stringResource(R.string.terms_contact_cta),
                        onClick = {
                            if (WA_SUPPORT_NUMBER.isNotEmpty()) {
                                val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/$WA_SUPPORT_NUMBER"))
                                context.startActivity(intent)
                            }
                        },
                        modifier = Modifier.width(140.dp),
                    )
                }

                // ── Footer ────────────────────────────────────────────────────
                Text(
                    text = stringResource(R.string.terms_footer),
                    style = MaterialTheme.typography.labelSmall,
                    color = OnSurfaceVariant.copy(alpha = 0.4f),
                    letterSpacing = 1.5.sp,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 12.dp),
                )
            }
        }
    }
}

@Composable
private fun TermsArticle(
    number: String,
    numberColor: androidx.compose.ui.graphics.Color,
    title: String,
    accentColor: androidx.compose.ui.graphics.Color,
    content: @Composable () -> Unit,
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(12.dp))
            .background(SurfaceContainerLowest)
            .padding(start = 4.dp),
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .clip(RoundedCornerShape(topStart = 0.dp, topEnd = 12.dp, bottomStart = 0.dp, bottomEnd = 12.dp))
                .background(SurfaceContainerLowest)
                .padding(24.dp),
        ) {
            // Left accent bar
            Box(
                modifier = Modifier
                    .align(Alignment.TopStart)
                    .offset(x = (-24).dp)
                    .width(4.dp)
                    .height(40.dp)
                    .background(accentColor)
            )
            Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.spacedBy(12.dp),
                ) {
                    Text(
                        text = number,
                        fontFamily = ManropeFontFamily,
                        fontWeight = FontWeight.ExtraBold,
                        fontSize = 32.sp,
                        color = numberColor,
                    )
                    Text(
                        text = title,
                        fontFamily = ManropeFontFamily,
                        fontWeight = FontWeight.Bold,
                        fontSize = 20.sp,
                        color = OnSurface,
                    )
                }
                content()
            }
        }
    }
}

@Composable
private fun ChecklistItem(text: String) {
    Row(
        horizontalArrangement = Arrangement.spacedBy(12.dp),
        verticalAlignment = Alignment.Top,
    ) {
        Icon(
            imageVector = Icons.Outlined.CheckCircle,
            contentDescription = null,
            tint = AccentBlue,
            modifier = Modifier.size(18.dp).padding(top = 2.dp),
        )
        Text(
            text = text,
            style = MaterialTheme.typography.bodyMedium,
            color = OnSurfaceVariant,
            lineHeight = 22.sp,
        )
    }
}
