package com.img.rabbit.pages.toolbar

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.img.rabbit.R

@Composable
fun TitleBar(navController: NavController?, paddingValues: PaddingValues, title: String? = "", showSave: Boolean = false, showBreak: Boolean = true, onSubmit: (() -> Unit)? = null) {
    Box(
        modifier = Modifier.fillMaxWidth().padding(paddingValues)
    ){
        Row(
            modifier = Modifier
                .fillMaxWidth().padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            if(showBreak){
                // 返回按钮
                Icon(
                    painter = painterResource(id = R.mipmap.ic_back),
                    contentDescription = "返回",
                    modifier = Modifier
                        .clickable(
                            indication = null,
                            interactionSource = remember { MutableInteractionSource() }
                        ) {
                            navController?.popBackStack()
                        }
                        .padding(end = 26.dp)
                )
            }
            Column(
                modifier = Modifier.fillMaxWidth().weight(1f)
            ) {

                Text(
                    text = title ?: "",
                    color = Color(0xFF1A1A1A),
                    fontSize = 16.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                )
            }

            if(showSave) {
                Box(
                    modifier = Modifier
                        .width(60.dp)
                        .height(36.dp)
                        .background(
                            Color(0xFF252525),
                            shape = RoundedCornerShape(359.dp),
                        )
                        .clickable(
                            indication = null,
                            interactionSource = remember { MutableInteractionSource() }
                        ) {
                            // 保存
                            onSubmit?.invoke()
                        }
                ) {
                    Text(
                        "保存",
                        color = Color(0xFFC2FF43),
                        fontSize = 16.sp,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier
                            .wrapContentWidth()
                            .wrapContentHeight()
                            .align(Alignment.Center)
                    )
                }
            }else{
                Box(
                    modifier = Modifier
                        .width(60.dp)
                        .height(36.dp)
                )
            }

        }
    }
}

@Preview(showBackground = true)
@Composable
private fun PreviewTitleBar() {
    TitleBar(navController = rememberNavController(), paddingValues = PaddingValues(), title = "截图兔", showSave = true)
}
