//
//  LogIn.swift
//  Comedores Sociales
//
//  Created by Hector Carrion on 10/24/20.
//

import SwiftUI

struct LoginView: View {
    @EnvironmentObject var loginVM: LoginViewModel
    
    @State var email: String = ""
    @State var password: String = ""
    @State var showingRegister: Bool = false
    @State private var showingAlert = false
    
    var body: some View {
        VStack {
            WelcomeImage()
            WelcomeText()
            EmailField(email: $email)
            PasswordField(password: $password)
            
            if loginVM.response == .failure {
                Text(NSLocalizedString("incorrect", comment:""))
                    .offset(y: -10)
                    .foregroundColor(.red)
            } else if loginVM.response == .success {
                Text("Login successful 🎉")
                    .offset(y: -10)
                    .foregroundColor(.green)
            } else if loginVM.response == .loading {
                ProgressView("Loading…")
                    .padding()
            }
            
            Button(action: {
                let authData = Authentication(email: email, password: password)
                if authData.isComplete() {
                    loginVM.login(data: authData)
                } else {
                    // alert
                    self.showingAlert = true
                }
                
            }) {
                LoginText()
            }
            
            Button(action: {self.showingRegister.toggle()}) {
                RegisterText()
            }
            .sheet(isPresented: $showingRegister, content: {RegisterView()})
            .alert(isPresented: $showingAlert) {
                Alert(title: Text(NSLocalizedString("incomplete", comment:"")), message: Text(NSLocalizedString("enter_valid", comment:"")), dismissButton: .default(Text("OK")))
                
            }
        }
        .padding()
    }
}


struct WelcomeText: View {
    var body: some View {
        Text("Comedores Sociales")
            .font(.largeTitle)
            .fontWeight(.semibold)
            .padding(.bottom, 20)
    }
}

struct WelcomeImage: View {
    var body: some View {
        Image("logInImage")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 150, height: 150)
            .clipped()
            //.cornerRadius(150)
            //.padding(.bottom, 75)
    }
}

struct LoginText: View {
    var body: some View {
        Text(NSLocalizedString("login", comment:""))
            .font(.headline)
            .foregroundColor(.white)
            .padding()
            .frame(width: 220, height: 50)
            .background(Color.blue)
            .cornerRadius(5)
            .padding(.bottom, 5)
    }
}

struct RegisterText: View {
    var body: some View {
        Text(NSLocalizedString("register", comment:""))
            .font(.headline)
            .foregroundColor(.gray)
            .padding()
            .frame(width: 350, height: 50)
            .background(Color.clear)
            .cornerRadius(5)
    }
}

struct EmailField: View {
    @Binding var email: String
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        TextField("Email", text: $email)
            .disableAutocorrection(true)
            .autocapitalization(.none)
            .padding()
            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
            .cornerRadius(5.0)
            .padding(.bottom, 5)
    }
}

struct PasswordField: View {
    @Binding var password: String
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        SecureField("Password", text: $password)
            .autocapitalization(.none)
            .padding()
            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
            .cornerRadius(5.0)
            .padding(.bottom, 20)
    }
}
