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

import SwiftUI

struct RegisterView: View {
    @EnvironmentObject var registerVM: RegisterViewModel
    
    @State var name: String = ""
    @State var phoneNumber: String = ""
    @State var email: String = ""
    @State var password: String = ""
    @State var passwordConf: String = ""
    @State var urb: String = ""
    @State var city: String = ""
    @State var street: String = ""
    @State var org: String = ""
    @State var position: String = ""
    @State var zip: String = ""
    @Environment(\.colorScheme) var colorScheme
    
    @State private var showingAlert = false
    @State private var passwordsAlert = false
    
    var body: some View {
        VStack {
            Text(NSLocalizedString("details", comment:""))
                .font(.largeTitle)
                .fontWeight(.semibold)
                .frame(width: 350, height: 25, alignment: .topLeading)
                .padding(.bottom, 20)
            
            ScrollView {
                Group {
                    Group {
                        TextField(NSLocalizedString("full_name", comment:""), text: $name)
                            .autocapitalization(.words)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        TextField("Email", text: $email)
                            .disableAutocorrection(true)
                            .autocapitalization(.none)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        SecureField("Password", text: $password)
                            .autocapitalization(.none)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        SecureField(NSLocalizedString("pass_conf", comment:""), text: $passwordConf)
                            .autocapitalization(.none)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                    }
                        TextField(NSLocalizedString("phone", comment:""), text: $phoneNumber)
                            .keyboardType(.numberPad)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        TextField(NSLocalizedString("urb", comment:""), text: $urb)
                            .padding()
                            .autocapitalization(.words)
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        TextField(NSLocalizedString("street", comment:""), text: $street)
                            .autocapitalization(.words)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        TextField(NSLocalizedString("city", comment:""), text: $city)
                            .autocapitalization(.words)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        TextField(NSLocalizedString("zip", comment:""), text: $zip)
                            .keyboardType(.numberPad)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        TextField(NSLocalizedString("org", comment:""), text: $org)
                            .autocapitalization(.words)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            .padding(.bottom, 5)
                        TextField(NSLocalizedString("pos", comment:""), text: $position)
                            .padding()
                            .background(colorScheme == .dark ? darkGreyColor : lightGreyColor)
                            .cornerRadius(5.0)
                            //.padding(.bottom, 5)
                    
                }.padding(5)
            }
            
            if registerVM.response == .failure {
                Text(NSLocalizedString("reg_fail", comment:""))
                    //.offset(y: -5)
                    .foregroundColor(.red)
            } else if registerVM.response == .loading {
                ProgressView("Loading…")
                    .padding()
            } else if registerVM.response == .success {
                Text("Registration successful 🎉")
                    //.offset(y: -5)
                    .foregroundColor(.green)
            }
            
            
            Button(action: {
                print("register tapped")
                
                let registrationData = Registration(email: email, password: password, name: name, urb: urb, street: street, city: city, zip: zip, phone: phoneNumber, org: org, position: position)
                
                if password == passwordConf {
                    if registrationData.isComplete() {
                        registerVM.register(data: registrationData)
                    } else {
                        // Alert here
                        self.showingAlert = true
                    }
                } else {
                    // Alert here
                    self.passwordsAlert = true
                }
                
                
            }) {
                Text(NSLocalizedString("reg", comment:""))
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .frame(width: 220, height: 50)
                    .background(Color.blue)
                    .cornerRadius(5)
                    .padding(.bottom, 5)
            }
            
            
        }.alert(isPresented: $showingAlert) {
            Alert(title: Text(NSLocalizedString("incomplete", comment:"")), message: Text(NSLocalizedString("enter_all", comment:"")), dismissButton: .default(Text("OK")))}
        .alert(isPresented: $passwordsAlert) {
            Alert(title: Text(NSLocalizedString("pass_missmatch", comment:"")), message: Text(NSLocalizedString("pass_retype", comment:"")), dismissButton: .default(Text("OK")))}
        .padding()
    }
}

struct RegisterView_Previews: PreviewProvider {
    static var previews: some View {
        RegisterView()
    }
}
