package com.aiosman.riderpro.ui.account

import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
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.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.ui.comment.NoticeScreenHeader
import com.aiosman.riderpro.ui.composables.ActionButton
import com.aiosman.riderpro.ui.composables.StatusBarSpacer
import com.aiosman.riderpro.ui.composables.TextInputField
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch

@Composable
fun ResetPasswordScreen() {
    var username by remember { mutableStateOf("") }
    val accountService: AccountService = AccountServiceImpl()
    val scope = rememberCoroutineScope()
    val context = LocalContext.current
    var isSendSuccess by remember { mutableStateOf<Boolean?>(null) }
    var isLoading by remember { mutableStateOf(false) }
    val navController = LocalNavController.current
    var usernameError by remember { mutableStateOf<String?>(null) }
    fun validate(): Boolean {
        if (username.isEmpty()) {
            usernameError = context.getString(R.string.text_error_email_required)
            return false
        }
        usernameError = null
        return true
    }
    fun resetPassword() {
        if (!validate()) return
        scope.launch {
            isLoading = true
            try {
                accountService.resetPassword(username)
                isSendSuccess = true
            } catch (e: Exception) {
                Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
                isSendSuccess = false
            } finally {
                isLoading = false
            }
        }
    }

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        StatusBarSpacer()
        Box(
            modifier = Modifier.padding(
                start = 16.dp,
                end = 16.dp,
                top = 16.dp,
                bottom = 0.dp
            )
        ) {
            NoticeScreenHeader(
                stringResource(R.string.recover_account_upper),
                moreIcon = false
            )
        }

        Column(
            modifier = Modifier.padding(horizontal = 24.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Spacer(modifier = Modifier.height(36.dp))

            if (isSendSuccess != null) {
                if (isSendSuccess!!) {
                    Spacer(modifier = Modifier.height(16.dp))
                    Text(
                        text = stringResource(R.string.reset_mail_send_success),
                        style = TextStyle(
                            color = Color(0xFF333333),
                            fontSize = 14.sp,
                            fontWeight = FontWeight.Bold
                        )
                    )
                } else {
                    Spacer(modifier = Modifier.height(16.dp))
                    Text(
                        text = stringResource(R.string.reset_mail_send_failed),
                        style = TextStyle(
                            color = Color(0xFF333333),
                            fontSize = 14.sp,
                            fontWeight = FontWeight.Bold
                        )
                    )
                }
                Spacer(modifier = Modifier.height(40.dp))
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.noRippleClickable {
                        navController.popBackStack()
                    }
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.rider_pro_nav_back),
                        contentDescription = "Back",
                        modifier = Modifier.size(24.dp)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    androidx.compose.material3.Text(
                        stringResource(R.string.back_upper),
                        color = Color.Black,
                        fontSize = 16.sp,
                        fontWeight = FontWeight.Bold
                    )
                }
            } else {
                Spacer(modifier = Modifier.height(120.dp))
                TextInputField(
                    text = username,
                    onValueChange = { username = it },
                    label = stringResource(R.string.login_email_label),
                    hint = stringResource(R.string.text_hint_email),
                    enabled = !isLoading,
                    error = usernameError
                )
                Spacer(modifier = Modifier.height(72.dp))
                if (isLoading) {
                    CircularProgressIndicator()
                } else {
                    ActionButton(
                        modifier = Modifier
                            .width(345.dp)
                            .height(48.dp),
                        text = stringResource(R.string.recover),
                        backgroundImage = R.mipmap.rider_pro_signup_red_bg
                    ) {
                        resetPassword()
                    }
                }
            }
        }
    }
}