//
//  ContactSupportVC.swift
//  WOKA
//
//  Created by Bilal on 04/06/2024.
//

import UIKit
import RSKPlaceholderTextView

class ContactSupportVC: UIViewController {

    @IBOutlet weak var supportGirlImage: UIImageView!

    @IBOutlet weak var bottomArrow: UIImageView!
    @IBOutlet weak var subjectMainStack: UIStackView!
    @IBOutlet weak var subjectStack: UIStackView!
    @IBOutlet weak var messageStack: UIStackView!
    @IBOutlet weak var subjectLabel: UILabel!
    @IBOutlet weak var messageTextView: RSKPlaceholderTextView!
    @IBOutlet weak var submitBtn: LocalisedElementsButton!
    
    @IBOutlet weak var emailStack: UIStackView!
    @IBOutlet weak var nameStack: UIStackView!
    @IBOutlet weak var emailTF: TextFieldShadow!
    @IBOutlet weak var nameTF: TextFieldShadow!
    @IBOutlet weak var assistanceLabel: UILabel!
    
    var vm = ContactSupportVM()
    
    // MARK: - View LifeCycle

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Woka Support"
        vm.vc = self
        vm.initView()
        
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    // MARK: - Button Click Handler

    @IBAction func submitBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        /*
         if user is guest check for name and email
         */
        guard let email = emailTF.text?.trimmingCharacters(in: .whitespaces), let name = nameTF.text?.trimmingCharacters(in: .whitespaces), let message = messageTextView.text?.trimmingCharacters(in: .whitespaces) else{return}
        if AuthFunc.shareInstance.getUserType() == 3{
            if emailTF.text != nil{
                //validate email fiirst
                let emailValidate = EmailValidation(email: email).validate()
                if  emailValidate != .isCorrect{
                    let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
                    let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
                    
                    vcPush.contentLabel = emailValidate.rawValue.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
                    vcPush.mainTitleText = "Email".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
                    vcPush.modalPresentationStyle = .overCurrentContext
                    vcPush.modalTransitionStyle = .crossDissolve
                    self.present(vcPush, animated: true)
                    return
                }
            }
            
            if name.count == 0{
                /*
                 Check for username
                 */
                let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
                let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
                
                vcPush.contentLabel = K.ConstantString.required.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
                vcPush.mainTitleText = "Name".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
                vcPush.modalPresentationStyle = .overCurrentContext
                vcPush.modalTransitionStyle = .crossDissolve
                self.present(vcPush, animated: true)
                return
            }
            
            if name.count < 3{
                /*
                 Check for username
                 */
                let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
                let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
                
                vcPush.contentLabel = K.ConstantString.shortName.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
                vcPush.mainTitleText = "Name".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
                vcPush.modalPresentationStyle = .overCurrentContext
                vcPush.modalTransitionStyle = .crossDissolve
                self.present(vcPush, animated: true)
                return
            }
        }
        
        if subjectLabel.text?.lowercased() == "select subject" || subjectLabel.text == nil{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            
            vcPush.contentLabel = "Please select subject".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = "Subject".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        if message.count == 0{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            vcPush.contentLabel = K.ConstantString.required.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = "Message".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        if message.count < 3{
            let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
            let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC
            vcPush.contentLabel = "Message is too short".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.mainTitleText = "Message".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
            vcPush.modalPresentationStyle = .overCurrentContext
            vcPush.modalTransitionStyle = .crossDissolve
            self.present(vcPush, animated: true)
            return
        }
        
        if AuthFunc.shareInstance.getUserType() == 3{ // He is Guest
            vm.guestContactSupportApiCall()
        }else{
            vm.contactSupportApiCall()
        }
    }
}

// MARK: - Textfield, TextView Delegate

extension ContactSupportVC : UITextFieldDelegate , UITextViewDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        switch textField{
        case emailTF:
            return ValidatorClass.sharedInstanec.limitCharacter(length: 255,textField, shouldChangeCharactersIn: range, replacementString: string)
        case nameTF:
            if !string.nameCharacterOnly(){return false}
            return ValidatorClass.sharedInstanec.limitCharacter(length: 50,textField, shouldChangeCharactersIn: range, replacementString: string)
        default:
            return true
        }
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if !text.nameCharacterOnly(){return false}
        return ValidatorClass.sharedInstanec.limitCharacterTV(length: 200,textView, shouldChangeCharactersIn: range, replacementString: text)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        switch textField{
        case emailTF:
            nameTF.becomeFirstResponder()
        case nameTF:
            textField.resignFirstResponder()
        default:
            break
        }
        return true
    }
    
}
