package com.ys.bdtp.adm.mvvm.ui.compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.ys.bdtp.adm.R
import com.ys.bdtp.adm.app.theme.textBlackColor
import com.ys.bdtp.adm.app.theme.whiteColor

private val contentPadding = PaddingValues()

private val leftShape = RoundedCornerShape(
    topStart = 4.dp,
    bottomStart = 4.dp,
    topEnd = 0.dp,
    bottomEnd = 0.dp
)
private val rightShape = RoundedCornerShape(
    topStart = 0.dp,
    bottomStart = 0.dp,
    topEnd = 4.dp,
    bottomEnd = 4.dp
)

sealed interface SelectState {
    object Left : SelectState
    object Right : SelectState
}

@Composable
fun SelectButton(initState: SelectState.Left, click: (SelectState) -> Unit) {
    var state by remember { mutableStateOf<SelectState>(initState) }
    var leftResId by remember { mutableStateOf(R.drawable.btn_bg_blue_left_close) }
    var rightResId by remember { mutableStateOf(R.drawable.btn_bg_gray_right_open) }

    Row(
        modifier = Modifier
            .width(360.dp)
            .height(32.dp)
            .background(Color.White, shape = RoundedCornerShape(4.dp))
    ) {
        TextButton(
            modifier = Modifier.weight(1f),
            onClick = {
                if (state != SelectState.Left) {
                    state = SelectState.Left
                    leftResId = R.drawable.btn_bg_blue_left_close
                    rightResId = R.drawable.btn_bg_gray_right_open
                    click(state)
                }
            },
            contentPadding = contentPadding,
            shape = leftShape,
            colors = ButtonDefaults.textButtonColors(
                backgroundColor = whiteColor,
                contentColor = textBlackColor
            )
        ) {
            Box {
                Image(
                    modifier = Modifier.fillMaxSize().align(Alignment.Center),
                    painter = painterResource(leftResId),
                    contentDescription = ""
                )
                Text(
                    modifier = Modifier.align(Alignment.Center),
                    text = "土建精装图纸核查",
                    fontSize = 14.sp
                )
            }
        }

        TextButton(
            modifier = Modifier.weight(1f),
            onClick = {
                if (state != SelectState.Right) {
                    state = SelectState.Right
                    leftResId = R.drawable.btn_bg_gray_left_open
                    rightResId = R.drawable.btn_bg_blue_right_close
                    click(state)
                }
            },
            contentPadding = contentPadding,
            shape = rightShape,
            colors = ButtonDefaults.textButtonColors(
                backgroundColor = whiteColor,
                contentColor = textBlackColor
            )
        ) {
            Box {
                Image(
                    modifier = Modifier.fillMaxSize().align(Alignment.Center),
                    painter = painterResource(rightResId),
                    contentDescription = ""
                )
                Text(
                    modifier = Modifier.align(Alignment.Center),
                    text = "机电管道图纸核查",
                    fontSize = 14.sp
                )
            }
        }
    }
}

@Preview
@Composable
fun PreviewSelectButton() {
    SelectButton(SelectState.Left, click = { state -> })
}