//
//  FindPasswordView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import SwiftUI

struct FindPasswordView: View {
    
    @StateObject var viewModel = FindPasswordViewModel()
    @StateObject var keyboardHandler = KeyboardHandler()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            GeometryReader { proxy in
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "비밀번호 재설정")
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        VStack(spacing: 0) {
                            Text("회원가입한 이메일 주소로\n임시 비밀번호를 보내드립니다.")
                                .font(.custom(Font.bold.rawValue, size: 16))
                                .foregroundColor(Color(hex: "eeeeee"))
                                .multilineTextAlignment(.center)
                                .lineSpacing(6)
                                .padding(.top, 40)
                                .padding(.horizontal, 26.7)
                            
                            Text("임시 비밀번호로 로그인 후, 마이페이지 > 프로필 설정에서\n비밀번호를 변경하고 이용하세요.")
                                .font(.custom(Font.medium.rawValue, size: 12))
                                .foregroundColor(Color(hex: "909090"))
                                .multilineTextAlignment(.center)
                                .lineSpacing(6)
                                .padding(.top, 40)
                                .padding(.horizontal, 26.7)
                            
                            UserTextField(
                                title: "이메일",
                                hint: "이메일 주소를 입력해 주세요",
                                isSecure: false,
                                variable: $viewModel.email,
                                keyboardType: .emailAddress
                            )
                            .padding(.top, 40)
                            .padding(.horizontal, 26.7)
                            
                            Text("임시 비밀번호 받기")
                                .font(.custom(Font.bold.rawValue, size: 18.3))
                                .foregroundColor(Color.white)
                                .frame(maxWidth: proxy.size.width - 26.7)
                                .padding(.vertical, 16)
                                .background(Color(hex: "9970ff"))
                                .cornerRadius(6.7)
                                .padding(.top, 60)
                                .onTapGesture { viewModel.findPassword() }
                            
                            HStack(spacing: 13.3) {
                                Image("ic_headphones_purple")
                                
                                Text("고객센터로 문의하기")
                                    .font(.custom(Font.medium.rawValue, size: 13.3))
                                    .foregroundColor(Color(hex: "9970ff"))
                            }
                            .padding(.vertical, 10.7)
                            .padding(.horizontal, 18.7)
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .stroke(Color(hex: "9970ff"), lineWidth: 1)
                            )
                            .padding(.top, 93)
                            .onTapGesture {
                                UIApplication.shared.open(URL(string: "http://pf.kakao.com/_lkxgxhG/chat")!)
                            }
                        }
                    }
                }
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .bottom, autohideIn: 2) {
            HStack {
                Spacer()
                Text(viewModel.errorMessage)
                    .padding(.vertical, 13.3)
                    .padding(.horizontal, 13.3)
                    .frame(width: screenSize().width - 66.7, alignment: .center)
                    .font(.custom(Font.medium.rawValue, size: 12))
                    .background(Color(hex: "9970ff"))
                    .foregroundColor(Color.white)
                    .multilineTextAlignment(.leading)
                    .cornerRadius(20)
                    .padding(.bottom, 66.7)
                Spacer()
            }
        }
    }
}

struct FindPasswordView_Previews: PreviewProvider {
    static var previews: some View {
        FindPasswordView()
    }
}
