package com.iscs.bozzys.ui.common

import android.app.Activity
import androidx.annotation.DrawableRes
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.shape.RoundedCornerShape
import androidx.compose.material3.Icon
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.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.iscs.bozzys.R

/**
 * 通用标题
 *
 * @param pv            页面顶部和底部占位
 * @param rightShow     右边按钮是否显示
 * @param rightIcon     右边icon
 * @param rightClick    右边点击事件
 */
@Composable
fun Title(pv: PaddingValues, title: String, rightShow: Boolean = false, @DrawableRes rightIcon: Int = 0, rightClick: () -> Unit = {}) {
    val ctx = LocalContext.current
    Column(
        Modifier
            .fillMaxWidth()
            .background(brush = Brush.horizontalGradient(listOf(Color(0xFFFF8C00), Color(0xFFFFA500))))
            .padding(top = pv.calculateTopPadding())
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .height(50.dp)
                .padding(horizontal = 16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                painter = painterResource(R.drawable.back),
                contentDescription = "",
                modifier = Modifier
                    .size(32.dp)
                    .clip(RoundedCornerShape(50))
                    .background(Color(0xFFFFA126))
                    .clickable(onClick = {
                        if (ctx is Activity) ctx.finish()
                    })
                    .padding(8.dp),
                tint = Color.White
            )
            Text(
                title,
                Modifier.weight(1f),
                fontSize = 18.sp,
                lineHeight = 50.sp,
                fontWeight = FontWeight.Bold,
                textAlign = TextAlign.Center,
                color = Color.White
            )
            if (rightShow) {
                Icon(
                    painter = painterResource(rightIcon),
                    contentDescription = null,
                    modifier = Modifier
                        .size(40.dp)
                        .clip(RoundedCornerShape(20))
                        .clickable(onClick = rightClick)
                        .padding(8.dp),
                    tint = Color.White
                )
            } else {
                Spacer(Modifier.size(40.dp))
            }
        }
    }
}