//
//  ModifyPasswordView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/19.
//

import SwiftUI

struct ModifyPasswordView: View {
    
    @StateObject var viewModel = ProfileUpdateViewModel()
    @StateObject var keyboardHandler = KeyboardHandler()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            GeometryReader { proxy in
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "비밀번호 변경")
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        Text("안전한 비밀번호로 내 내 정보를 보호하세요")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(Color.grayee)
                            .padding(.top, 40)
                        
                        VStack(spacing: 26.7) {
                            UserTextField(
                                title: "현재 비밀번호",
                                hint: "현재 비밀번호를 입력하세요.",
                                isSecure: true,
                                variable: $viewModel.currentPassword
                            )
                            
                            UserTextField(
                                title: "신규 비밀번호",
                                hint: "신규 비밀번호를 입력해주세요(영문, 숫자 포함 8자 이상)",
                                isSecure: true,
                                variable: $viewModel.newPassword
                            )
                            
                            UserTextField(
                                title: "신규 비밀번호 확인",
                                hint: "신규 비밀번호를 재입력해주세요",
                                isSecure: true,
                                variable: $viewModel.newPasswordConfirm
                            )
                        }
                        .padding(.top, 40)
                        .frame(width: screenSize().width - 53.4)
                        
                        Text("* 영문, 숫자 포함 8자 이상")
                            .font(.custom(Font.medium.rawValue, size: 12))
                            .foregroundColor(Color.mainRed3)
                            .frame(width: screenSize().width - 53.4, alignment: .leading)
                            .padding(.top, 13.7)
                    }
                    
                    if !viewModel.isLoading {
                        Text("비밀번호 변경하기")
                            .font(.custom(Font.bold.rawValue, size: 18.3))
                            .foregroundColor(.white)
                            .padding(.vertical, 16)
                            .frame(width: screenSize().width - 26.7)
                            .background(Color.button)
                            .cornerRadius(10)
                            .padding(.vertical, 13.7)
                            .frame(width: screenSize().width)
                            .background(Color.gray22)
                            .cornerRadius(16.7, corners: [.topLeft, .topRight])
                            .onTapGesture {
                                hideKeyboard()
                                viewModel.updatePassword()
                            }
                        
                        if proxy.safeAreaInsets.bottom > 0 {
                            Rectangle()
                                .foregroundColor(Color.gray22)
                                .frame(width: proxy.size.width, height: 15.3)
                        }
                    }
                }
                .edgesIgnoringSafeArea(.bottom)
                .onTapGesture {
                    hideKeyboard()
                }
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .padding(.horizontal, 6.7)
                        .frame(width: geo.size.width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.leading)
                        .fixedSize(horizontal: false, vertical: true)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
    }
}
