//
//  SignupViewController.swift
//  firetail
//
//  Created by Aaron Halvorsen on 2/27/17.
//  Copyright © 2017 Aaron Halvorsen. All rights reserved.
//

import UIKit

import Firebase
import FirebaseAuth
import ReachabilitySwift

class SignupViewController: ViewSetup, UITextFieldDelegate {
    var customColor = CustomColor()
    @objc var login = UIButton()
    @objc var username = UILabel()
    @objc var continueB = UIButton()
    @objc var createAccount = UILabel()
    @objc var textFields = [UITextField]()
    let loadsave = LoadSaveCoreData()
    @objc var tap = UITapGestureRecognizer()
    let alphaAPI = Alpha()
    
    //var ti = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()
        Set2.priceRectX.removeAll()
        alphaAPI.populateSet1Month()
        for xVal in Set3.shared.priceRectX {
            
            Set2.priceRectX.append(xVal*screenWidth/375)
        }
        tap = UITapGestureRecognizer(target: self, action: #selector(SignupViewController.dismissKeyboard(_:)))
        view.backgroundColor = customColor.black33
        let logo = UIImageView(frame: CGRect(x: screenWidth/2 - 93*screenHeight/1334, y: 42*screenHeight/667, width: 93*screenHeight/667, height: 119*screenHeight/667))
        logo.image = #imageLiteral(resourceName: "logo93x119")
        view.addSubview(logo)
        let accountView = UIView(frame: CGRect(x: 0, y: 266*screenHeight/667, width: screenWidth, height: 401*screenHeight/667))
        accountView.backgroundColor = customColor.black30
        view.addSubview(accountView)
        addButton(name: login, x: 0, y: 400, width: 254, height: 76, title: "LOGIN", font: "Roboto-Bold", fontSize: 15, titleColor: .white, bgColor: .clear, cornerRad: 0, boarderW: 0, boarderColor: .clear, act: #selector(SignupViewController.loginFunc(_:)), addSubview: true, alignment: .center)
        addLabel(name: username, text: "", textColor: .white, textAlignment: .right, fontName: "Roboto-Medium", fontSize: 15, x: 200, y: 400, width: 470, height: 76, lines: 1)
        username.alpha = 0.1
        view.addSubview(username)
        addButton(name: continueB, x: 0, y: 1194, width: 750, height: 140, title: "CONTINUE", font: "Roboto-Bold", fontSize: 17, titleColor: .white, bgColor: customColor.black24, cornerRad: 0, boarderW: 0, boarderColor: .clear, act: #selector(SignupViewController.continueFunc(_:)), addSubview: true, alignment: .center)
        let arrowView = UIImageView(frame: CGRect(x: screenWidth - 70*screenHeight/667, y: 597*screenHeight/667, width: 70*screenHeight/667, height: 70*screenHeight/667))
        arrowView.image = #imageLiteral(resourceName: "forwardarrow")
        view.addSubview(arrowView)
        addLabel(name: createAccount, text: "CREATE ACCOUNT", textColor: .white, textAlignment: .left, fontName: "Roboto-Bold", fontSize: 15, x: 80, y: 624, width: 360, height: 30, lines: 1)
        view.addSubview(createAccount)
        for i in 0...2 {
            let line = UILabel(frame: CGRect(x: 38*screenWidth/375, y: 408*screenHeight/667 + CGFloat(i)*60*screenHeight/667, width: 300*screenWidth/375, height: 2*screenHeight/667))
            line.backgroundColor = customColor.fieldLines
            view.addSubview(line)
            
            
            var myTextField = UITextField()
            myTextField = UITextField(frame: CGRect(x: 38*screenWidth/375,y: 374*screenHeight/667 + CGFloat(i)*61*screenHeight/667,width: 300*screenWidth/375 ,height: 34*screenHeight/667))
            switch i {
            case 0:
                myTextField.placeholder = "email@address.com"
            case 1:
                myTextField.isSecureTextEntry = true
                myTextField.placeholder = "Password"
            default:
                myTextField.isSecureTextEntry = true
                myTextField.placeholder = "Confirm Password"
            }
            myTextField.setValue(UIColor.white, forKeyPath: "_placeholderLabel.textColor")
            myTextField.autocorrectionType = UITextAutocorrectionType.no
            myTextField.keyboardType = UIKeyboardType.default
            myTextField.returnKeyType = UIReturnKeyType.done
            myTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
            myTextField.delegate = self
            myTextField.backgroundColor = .clear
            myTextField.textColor = .white
            myTextField.tag = i
            myTextField.font = UIFont(name: "Roboto-Italic", size: 15)
            myTextField.keyboardAppearance = .dark
            view.addSubview(myTextField)
            textFields.append(myTextField)
        }
    }
    
    @objc private func loginFunc(_ sender: UIButton) {
        self.performSegue(withIdentifier: "fromSignupToLogin", sender: self)
        
    }
    
    func textFieldDidBeginEditing(_ textField : UITextField)
    {
        view.addGestureRecognizer(tap)
        switch textField.tag {
        case 0:
            view.frame.origin.y = -50*screenHeight/1334
        case 1:
            view.frame.origin.y = -170*screenHeight/1334
        case 2:
            view.frame.origin.y = -170*screenHeight/1334
            
            if UIDevice().userInterfaceIdiom == .phone {
                switch UIScreen.main.nativeBounds.height {
               
                case 2436:
                    print("iPhone X")
                    view.frame.origin.y = -290*screenHeight/1334
                default:
                    print("unknown")
                }
            }
            
        default:
            break
        }
        
        textField.autocorrectionType = .no
        textField.autocapitalizationType = .none
        textField.spellCheckingType = .no
    }
    @objc var continueOnce = true
    @objc private func continueFunc(_ sender: UIButton) {
        guard continueOnce == true else {return}
        continueOnce = false
        
        
        var cleanString = textFields[0].text!
        cleanString = cleanString.replacingOccurrences(of: ".", with: ",")
        cleanString = cleanString.replacingOccurrences(of: "$", with: "")
        cleanString = cleanString.replacingOccurrences(of: "#", with: "")
        cleanString = cleanString.replacingOccurrences(of: "[", with: "(")
        cleanString = cleanString.replacingOccurrences(of: "]", with: ")")
        cleanString = cleanString.replacingOccurrences(of: "/", with: "")
        
        loadsave.saveUsername(username: cleanString)
        Set1.username = cleanString
        Set1.email = textFields[0].text!
        let emailField = textFields[0].text
        let passwordField = textFields[1].text
        let passwordField2 = textFields[2].text
        
        guard let email = emailField else {continueOnce = true; return}
        guard let password1 = passwordField else {continueOnce = true; return}
        guard let password2 = passwordField2 else {continueOnce = true; return}
        guard password1 == password2 else {
            continueOnce = true
            let alert = UIAlertController(title: "Warning", message: "Passwords Do Not Match", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            return
        }
        guard email.isEmail == true else {
            continueOnce = true
            let alert = UIAlertController(title: "Warning", message: "Enter Valid Email Address", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            return
        }
        guard password1.isValidPassword == true else {
            continueOnce = true
            let alert = UIAlertController(title: "Invalid Password", message: "6-20 Characters", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
            return
        }
        
        Auth.auth().createUser(withEmail: email, password: password1) {
            user, error in
            if error == nil {
                self.continueOnce = true
                self.username.text = email
                Auth.auth().signIn(withEmail: email, password: password1) //adds authentication
                Set1.saveUserInfo()
                self.delay(bySeconds: 1.5) {
                    self.performSegue(withIdentifier: "fromSignupToAddStockTicker", sender: self)
                }
            } else {
                print("error not nil")
                
                Auth.auth().signIn(withEmail: email, password: password1, completion: { (user, error) in
                    if error != nil{
                        self.continueOnce = true
                    } else {
                        self.continueOnce = true
                        self.username.text = email
                        Set1.saveUserInfo()
                            self.performSegue(withIdentifier: "fromSignupToAddStockTicker", sender: self)
                        
                    }
                })
                
                
               // self.userWarning(title: "", message: (error!.localizedDescription))
            }
        }
    }
    
    @objc private func dismissKeyboard(_ gesture: UITapGestureRecognizer) {
        textFields[0].resignFirstResponder()
        textFields[1].resignFirstResponder()
        self.view.frame.origin.y = 0
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        view.frame.origin.y = 0
        view.removeGestureRecognizer(tap)
        self.view.endEditing(true)
        if textField.text != nil && textField.delegate != nil {
            
            //do something with the --> textField.text!
            
        }
        
        switch textField.tag {
        case 0:
            textFields[1].becomeFirstResponder()
        case 1:
            textFields[2].becomeFirstResponder()
        case 2:
            view.frame.origin.y = 0
            continueFunc(continueB)
        default:
            break
        }
        return false
    }
    
    override func viewDidAppear(_ animated: Bool) {
        reachabilityAddNotification()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        reachabilityRemoveNotification()
    }
    
    
}
