package de.sebse.fuplanner2.ui.details.components

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import de.sebse.fuplanner2.R
import de.sebse.fuplanner2.database.Lecturer
import de.sebse.fuplanner2.ui.shared.FuCardRow
import de.sebse.fuplanner2.ui.theme.AppTheme
import de.sebse.fuplanner2.ui.tools.previews.LecturerPreviewProvider

@Composable
fun LecturerItem(lecturer: Lecturer, courseTitle: String) {
    val mailTemplate = stringResource(R.string.email_preview, lecturer.firstName, lecturer.lastName)
    val fullName = stringResource(R.string.send_email, lecturer.firstName, lecturer.lastName)
    val ctx = LocalContext.current
    LecturerItem(lecturer) {
        sendMail(ctx, mailTemplate, fullName, lecturer.email, courseTitle)
    }
}

@Composable
fun LecturerItem(lecturer: Lecturer, click: () -> Unit) {
    FuCardRow(
        modifier = Modifier
            .clickable(true, onClick = click)
            .height(IntrinsicSize.Min)
    ) {
        Column(
            modifier = Modifier
                .weight(1f)
        ) {
            val textDecor = if (lecturer.isResponsible) TextDecoration.Underline else null
            Text(
                text = stringResource(
                    R.string.full_name,
                    lecturer.firstName,
                    lecturer.lastName
                ),
                textDecoration = textDecor,
                style = MaterialTheme.typography.h6
            )
            Text(
                text = lecturer.email,
                style = MaterialTheme.typography.subtitle1
            )
        }
        Column {
            Icon(
                Icons.Filled.Email,
                contentDescription = stringResource(R.string.mail_icon),
                tint = MaterialTheme.colors.secondary,
                modifier = Modifier
                    .fillMaxHeight()
                    .aspectRatio(1f, true)
            )
        }
    }
}

@Preview
@Composable
fun LecturerItemPreview(@PreviewParameter(LecturerPreviewProvider::class, 3) lecturer: Lecturer) {
    AppTheme {
        LecturerItem(lecturer, "Course Name")
    }
}

private fun sendMail(
    ctx: Context,
    mailTemplate: String,
    fullName: String,
    mail: String,
    courseTitle: String
) {
    val intent = Intent(Intent.ACTION_SENDTO)
    intent.type = "text/html"
    intent.data = Uri.fromParts("mailto", mail, null)
    intent.putExtra(Intent.EXTRA_SUBJECT, courseTitle)
    intent.putExtra(
        Intent.EXTRA_TEXT,
        mailTemplate
    )
    ctx.startActivity(
        Intent.createChooser(
            intent,
            fullName
        )
    )
}
