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

import SwiftUI
import PopupView

struct LoginView: View {
    
    @ObservedObject var viewModel = LoginViewModel()
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            VStack(spacing: 0) {
                HomeNavigationBar(title: "로그인") {}
                
                Spacer()
                
                UserTextField(
                    title: "이메일",
                    hint: "이메일 주소를 입력해 주세요",
                    isSecure: false,
                    variable: $viewModel.email,
                    keyboardType: .emailAddress
                )
                .padding(.horizontal, 26.7)
                
                UserTextField(
                    title: "비밀번호",
                    hint: "비밀번호를 입력해 주세요",
                    isSecure: true,
                    variable: $viewModel.password,
                    isPasswordVisibleButton: true
                )
                .padding(.top, 33.3)
                .padding(.horizontal, 26.7)
                
                Button(action: { 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)
                
                HStack(spacing: 10) {
                    Text("비밀번호 재설정")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.graybb)
                        .onTapGesture { AppState.shared.setAppStep(step: .findPassword) }
                    
                    Text("|")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.graybb)
                    
                    Text("회원가입")
                        .font(.custom(Font.medium.rawValue, size: 13.3))
                        .foregroundColor(Color.graybb)
                        .onTapGesture { AppState.shared.setAppStep(step: .signUp) }
                }
                .padding(.top, 40)
                
                Spacer()
            }
        }
        .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()
    }
}
