//
//  AddNewAddressVC.swift
//  WOKA
//
//  Created by MacBook Pro on 29/07/24.
//

import UIKit

class AddNewAddressVC: UIViewController {

    @IBOutlet weak var enterAddressNAmeTF: TextFieldShadow! // min - 2 , max - 50
    @IBOutlet weak var addressLine1TF: TextFieldShadow!
    @IBOutlet weak var addressLine2TF: TextFieldShadow!
    @IBOutlet weak var cityTF: TextFieldShadow!
    @IBOutlet weak var stateTF: TextFieldShadow!
    @IBOutlet weak var pinCodeTF: TextFieldShadow!
    @IBOutlet weak var countryTF: TextFieldShadow!
    @IBOutlet weak var phoneNumberTF: TextFieldShadow!
    
    var vm = AddNewAddressVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        self.navigationController?.setColor(color: .white)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        // Customize the navigation bar's appearance
        self.navigationController?.setColor(color: .black)
    }
    @IBAction func submitBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addShopCount(postID: 0)
        
        
        if let nameTF = enterAddressNAmeTF.text?.trimmingCharacters(in: .whitespaces) ,nameTF.count < 2 {
            Utilities.alertWithBtn(title: "", msgBody: "Name should be minimum 2 Characters.", okBtnStr: "OK", vc: self)
            return
        }
        
        if let addressLine1TF = addressLine1TF.text?.trimmingCharacters(in: .whitespaces) , addressLine1TF.count < 15 {
            Utilities.alertWithBtn(title: "", msgBody: "Address Line 1 should be minimum 15 Characters.", okBtnStr: "OK", vc: self)
            return
        }
        if let addressLine2TF = addressLine2TF.text?.trimmingCharacters(in: .whitespaces), addressLine2TF.count < 15 {
            Utilities.alertWithBtn(title: "", msgBody: "Address Line 2 should be minimum 15 Characters.", okBtnStr: "OK", vc: self)
            return
        }
        
        if let cityTF = cityTF.text?.trimmingCharacters(in: .whitespaces), cityTF.count < 2 {
            Utilities.alertWithBtn(title: "", msgBody: "City should be minimum 2 Characters.", okBtnStr: "OK", vc: self)
            return
        }
        
        if let stateTF = stateTF.text?.trimmingCharacters(in: .whitespaces), stateTF.count < 2 {
            Utilities.alertWithBtn(title: "", msgBody: "State should be minimum 2 Characters.", okBtnStr: "OK", vc: self)
            return
        }
        
        if let countryTF = countryTF.text?.trimmingCharacters(in: .whitespaces), countryTF.count < 2 {
            Utilities.alertWithBtn(title: "", msgBody: "Country should be minimum 2 Characters.", okBtnStr: "OK", vc: self)
            return
        }
        
        if phoneNumberTF.text!.count < 10 {
            Utilities.alertWithBtn(title: "", msgBody: "Enter 10 digit mobile number", okBtnStr: "OK", vc: self)
            return
        }
        
        self.vm.addAddress()
    }
}

extension AddNewAddressVC : UITextFieldDelegate{
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        switch textField{
        case enterAddressNAmeTF:
            if !string.nameCharacterOnly(){return false}
            // min - 2 , max - 50
            return ValidatorClass.sharedInstanec.limitCharacter(length: 50,textField, shouldChangeCharactersIn: range, replacementString: string)
        case addressLine1TF, addressLine2TF:
            if !string.numberAndCharacter(){return false}
            // min - 2 , max - 200
            return ValidatorClass.sharedInstanec.limitCharacter(length: 200,textField, shouldChangeCharactersIn: range, replacementString: string)
        case cityTF, stateTF, countryTF:
            if !string.nameCharacterOnly(){return false}
            // min - 2 , max - 200
            return ValidatorClass.sharedInstanec.limitCharacter(length: 50,textField, shouldChangeCharactersIn: range, replacementString: string)
        case phoneNumberTF:
            if !string.onlyNumber(){return false}
            // max - 10
            return ValidatorClass.sharedInstanec.limitCharacter(length: 10,textField, shouldChangeCharactersIn: range, replacementString: string)
        default:
            break
        }
        return true
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        switch textField{
        case enterAddressNAmeTF:
            addressLine1TF.becomeFirstResponder()
        case addressLine1TF:
            addressLine2TF.becomeFirstResponder()
        case addressLine2TF:
            cityTF.becomeFirstResponder()
        case cityTF:
            stateTF.becomeFirstResponder()
        case stateTF:
            countryTF.becomeFirstResponder()
        case countryTF:
            phoneNumberTF.becomeFirstResponder()
        default:
            break
        }
        return true
    }
}
