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

import SwiftUI
import PopupView

struct LoginView: View {
    
    enum FocusField: Hashable {
        case email, password
    }
    
    @ObservedObject var viewModel = LoginViewModel()
    
    @State private var isPasswordVisible: Bool = false
    @FocusState private var focusedField: FocusField?
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                DetailNavigationBar(title: "로그인")
                
                Spacer()
                
                TextField("이메일", text: $viewModel.email)
                    .submitLabel(.next)
                    .focused($focusedField, equals: .email)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    .font(.custom(Font.medium.rawValue, size: 15))
                    .foregroundColor(.grayee)
                    .keyboardType(.emailAddress)
                    .padding(.vertical, 18)
                    .padding(.horizontal, 13.3)
                    .frame(height: 56)
                    .background(RoundedRectangle(cornerRadius: 6.7).fill(Color.gray33.opacity(0.7)))
                    .padding(.horizontal, 13.3)
                    .onSubmit {
                        self.focusedField = .password
                    }
                    .onTapGesture {
                        self.focusedField = .email
                    }
                
                HStack {
                    Group {
                        if isPasswordVisible {
                            TextField("비밀번호", text: $viewModel.password)
                        } else {
                            SecureField("비밀번호", text: $viewModel.password)
                        }
                    }
                    .submitLabel(.done)
                    .focused($focusedField, equals: .password)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
                    .font(.custom(Font.medium.rawValue, size: 15))
                    .foregroundColor(.grayee)
                    
                    Spacer()
                    
                    Image(systemName: isPasswordVisible ? "eye.slash.fill" : "eye.fill")
                        .foregroundColor(.grayee)
                        .onTapGesture {
                            isPasswordVisible.toggle()
                        }
                }
                .padding(.vertical, 18)
                .padding(.horizontal, 13.3)
                .frame(height: 56)
                .background(RoundedRectangle(cornerRadius: 6.7).fill(Color.gray33.opacity(0.7)))
                .padding(.horizontal, 13.3)
                .padding(.top, 16)
                .onTapGesture {
                    self.focusedField = .password
                }
                
                Button(
                    action: {
                        hideKeyboard()
                        viewModel.login()
                    }
                ) {
                    Text("로그인")
                        .font(.custom(Font.bold.rawValue, size: 15))
                        .frame(width: screenSize().width - 26.6, height: 46.7)
                        .foregroundColor(.white)
                        .background(Color.button)
                        .cornerRadius(6.7)
                }
                .padding(.top, 40)
                
                Text("비밀번호를 잊으셨나요?")
                    .font(.custom(Font.medium.rawValue, size: 13.3))
                    .foregroundColor(Color.graybb)
                    .padding(.vertical, 10)
                    .onTapGesture {
                        hideKeyboard()
                        AppState.shared.setAppStep(step: .findPassword)
                    }
                    .padding(.top, 30)
                
                Text("보이스온 회원이 아닌가요? 지금 가입하세요.")
                    .font(.custom(Font.medium.rawValue, size: 13.3))
                    .foregroundColor(Color.graybb)
                    .padding(.vertical, 10)
                    .onTapGesture {
                        hideKeyboard()
                        AppState.shared.setAppStep(step: .signUp)
                    }
                    .padding(.top, 20)
                
                Spacer()
            }
            .onAppear {
                self.focusedField = .email
            }
        }
        .popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
            GeometryReader { geo in
                HStack {
                    Spacer()
                    Text(viewModel.errorMessage)
                        .padding(.vertical, 13.3)
                        .frame(width: geo.size.width - 66.7, alignment: .center)
                        .font(.custom(Font.medium.rawValue, size: 12))
                        .background(Color.button)
                        .foregroundColor(Color.white)
                        .multilineTextAlignment(.center)
                        .cornerRadius(20)
                        .padding(.top, 66.7)
                    Spacer()
                }
            }
        }
    }
}

struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        LoginView()
    }
}