package com.aiosman.riderpro.ui.login

import android.content.ContentValues.TAG
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.fillMaxWidth
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.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
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.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.riderpro.AppState
import com.aiosman.riderpro.AppStore
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.Messaging
import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.data.ServiceException
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.composables.ActionButton
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import com.aiosman.riderpro.utils.GoogleLogin
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


@Composable
fun SignupScreen() {
    val navController = LocalNavController.current
    val context = LocalContext.current
    val coroutineScope = rememberCoroutineScope()
    val accountService: AccountService = AccountServiceImpl()

    fun googleLogin() {
        coroutineScope.launch {
            try {

                GoogleLogin(context) {
                    coroutineScope.launch {
                        try {
                            accountService.regiterUserWithGoogleAccount(it)
                        } catch (e: Exception) {
                            Log.e(TAG, "Failed to register with google", e)
                            return@launch
                        }
                        // 获取用户信息
                        // 获取 token
                        val authResp = accountService.loginUserWithGoogle(it)
                        if (authResp.token != null) {
                            coroutineScope.launch(Dispatchers.Main) {
                                Toast.makeText(
                                    context,
                                    "Successfully registered",
                                    Toast.LENGTH_SHORT
                                )
                                    .show()
                            }
                        }
                        AppStore.apply {
                            token = authResp.token
                            this.rememberMe = true
                            saveData()
                        }
                        // 获取token 信息
                        try {
                            val resp = accountService.getMyAccount()
                            AppState.UserId = resp.id
                            Messaging.InitFCM(coroutineScope)
                        } catch (e: ServiceException) {
                            coroutineScope.launch(Dispatchers.Main) {
                                Toast.makeText(context, "Failed to get account", Toast.LENGTH_SHORT)
                                    .show()
                            }
                        }
                        coroutineScope.launch(Dispatchers.Main) {
                            navController.navigate(NavigationRoute.Index.route) {
                                popUpTo(NavigationRoute.Login.route) { inclusive = true }
                            }
                        }
                    }

                }
            } catch (e: Exception) {
                Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
            }

        }
    }


    Scaffold(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0xFFECECEC))
    ) {
        it
        Box(
            modifier = Modifier.fillMaxSize()
        ) {
            // to bottom
            Box(
                contentAlignment = Alignment.TopCenter,
                modifier = Modifier.padding(top = 211.dp)
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Image(
                        painter = painterResource(id = R.mipmap.rider_pro_logo),
                        contentDescription = "Rider Pro",
                        modifier = Modifier
                            .width(108.dp)
                            .height(45.dp)
                    )
                    Spacer(modifier = Modifier.height(32.dp))
                    Text(
                        "Connecting Riders".uppercase(),
                        fontSize = 28.sp,
                        fontWeight = FontWeight.Bold
                    )
                    Text("Worldwide".uppercase(), fontSize = 28.sp, fontWeight = FontWeight.Bold)
                }

            }
            Box(
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .padding(bottom = 82.dp, start = 24.dp, end = 24.dp)

            ) {
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    ActionButton(
                        modifier = Modifier
                            .width(345.dp)
                            .height(48.dp),
                        text = stringResource(R.string.sign_in_with_email),
                        backgroundImage = R.mipmap.rider_pro_signup_red_bg,
                        leading = {
                            Image(
                                painter = painterResource(id = R.mipmap.rider_pro_email),
                                contentDescription = "Email",
                                modifier = Modifier.size(24.dp)
                            )
                            Spacer(modifier = Modifier.width(8.dp))
                        }
                    ) {
                        navController.navigate(NavigationRoute.EmailSignUp.route)
                    }
//                    Spacer(modifier = Modifier.height(16.dp))
//                    ActionButton(
//                        modifier = Modifier
//                            .width(345.dp)
//                            .height(48.dp),
//                        text = "CONTINUE WITH FACEBOOK".uppercase(),
//                        backgroundImage = R.mipmap.rider_pro_signup_facebook_bg,
//                        leading = {
//                            Image(
//                                painter = painterResource(id = R.mipmap.rider_pro_signup_facebook),
//                                contentDescription = "Facebook",
//                                modifier = Modifier.size(24.dp)
//                            )
//                            Spacer(modifier = Modifier.width(8.dp))
//                        }
//                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    ActionButton(
                        modifier = Modifier
                            .width(345.dp)
                            .height(48.dp),
                        color = Color.Black,
                        leading = {
                            Image(
                                painter = painterResource(id = R.mipmap.rider_pro_signup_google),
                                contentDescription = "Google",
                                modifier = Modifier.size(24.dp)
                            )
                            Spacer(modifier = Modifier.width(8.dp))
                        },
                        text = stringResource(R.string.sign_in_with_google),
                        backgroundImage = R.mipmap.rider_pro_signup_white_bg
                    ) {
                        googleLogin()

                    }
//                    Spacer(modifier = Modifier.height(16.dp))
//                    Box(
//                        modifier = Modifier
//                            .width(345.dp)
//                            .height(48.dp)
//                            .clip(RoundedCornerShape(8.dp))
//                            .background(Color.Black)
//
//                    ) {
//                        Text(
//                            "Sign in with Apple",
//                            color = Color.White,
//                            modifier = Modifier.align(Alignment.Center),
//                            fontSize = 16.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))
                        Text(
                            stringResource(R.string.back_upper),
                            color = Color.Black,
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Bold
                        )
                    }
                }
            }
        }

    }
}