//
//  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 == false {
                Text("Information incorrect, please try again")
                    .offset(y: -10)
                    .foregroundColor(.red)
            } else if loginVM.response == true {
                Text("Login successful 🎉")
                    .offset(y: -10)
                    .foregroundColor(.green)
            }
            
            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("Fields incomplete"), message: Text("Please enter a valid email and password"), 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("Login")
            .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("Don't have an account? Register")
            .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)
            .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)
    }
}
