package com.isidroid.c23.ui.screen.render_preview._component

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontFamily
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 com.isidroid.c23.R
import com.isidroid.c23.ui.screen.render_preview.ext.getOrientationIcon
import com.isidroid.c23.ui.screen.render_preview.ext.getOrientationTitle
import com.isidroid.c23.ui.screen.render_preview.ext.getPrintSizeIcon
import com.isidroid.c23.ui.screen.render_preview.ext.getPrintSizeTitle
import com.isidroid.rendering.model.RenderSettingsV2

@Composable
fun PrintOptionsComponent(
    renderSettingsV2: RenderSettingsV2,
    clickOnCopies: () -> Unit,
    clickOnPrintSize: () -> Unit,
    clickOnOrientation: () -> Unit,
    modifier: Modifier = Modifier
) {
    Column(modifier = modifier.fillMaxWidth()) {
        Text(
            text = stringResource(id = R.string.printing_options),
            modifier = Modifier
                .background(MaterialTheme.colorScheme.tertiaryContainer)
                .fillMaxWidth()
                .padding(vertical = 12.dp, horizontal = 24.dp),
            fontSize = 16.sp,
            fontFamily = FontFamily.Serif,
            fontWeight = FontWeight.Bold
        )

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 12.dp)
                .padding(top = 24.dp, bottom = 12.dp)
        ) {
            PrintOptionItemComponent(
                modifier = Modifier
                    .weight(1f)
                    .clickable { clickOnCopies() },
                icon = R.drawable.ic_option_copies,
                counter = renderSettingsV2.copies,
                titleRes = R.string.copies,
            )

            PrintOptionItemComponent(
                modifier = Modifier
                    .weight(1f)
                    .clickable { clickOnPrintSize() },
                icon = getPrintSizeIcon(renderSettingsV2.printSize),
                titleRes = getPrintSizeTitle(renderSettingsV2.printSize)
            )

            PrintOptionItemComponent(
                modifier = Modifier
                    .weight(1f)
                    .clickable { clickOnOrientation() },
                icon = getOrientationIcon(renderSettingsV2.orientation),
                titleRes = getOrientationTitle(renderSettingsV2.orientation)
            )
        }
    }
}

@Composable
private fun PrintOptionItemComponent(
    modifier: Modifier,
    @StringRes titleRes: Int,
    @DrawableRes icon: Int,
    counter: Int? = null,
) {
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Box(
            modifier = Modifier
                .padding(bottom = 16.dp)
                .size(48.dp),
            contentAlignment = Alignment.Center
        ) {
            Image(
                painter = painterResource(id = icon),
                contentDescription = null,
                modifier = Modifier.size(48.dp)
            )

            if (counter != null)
                Text(text = "$counter", fontSize = 20.sp)
        }

        Text(
            text = stringResource(id = titleRes),
            fontSize = 16.sp
        )
    }
}


@Composable
@Preview
private fun PrintOptionsComponentPreview() {
    Surface {
        PrintOptionsComponent(
            renderSettingsV2 = RenderSettingsV2(),
            clickOnCopies = {},
            clickOnPrintSize = {},
            clickOnOrientation = {},
        )
    }
}