//
//  SideMenuVC.swift
//  WOKA
//
//  Created by MacBook Pro on 21/05/24.
//

import UIKit

class SideMenuVC: UIViewController {

    @IBOutlet weak var logoutBtn: UIButton!
    @IBOutlet weak var selectionMenuTrailingConstraint: NSLayoutConstraint!
    @IBOutlet weak var languageControl: CustomizableSegmentControl!
    
    @IBOutlet weak var theme1: UIView!
    @IBOutlet weak var theme2: UIView!
    
    //Theme Select Outlets
    @IBOutlet weak var themeOneView: UIView!
    @IBOutlet weak var themeOneCheckMark: UIImageView!
    @IBOutlet weak var themeTwoView: UIView!
    @IBOutlet weak var themetwoCheckMark: UIImageView!
    @IBOutlet weak var userName: UILabel!
    
    @IBOutlet weak var aboutBtn: UIButton!
    @IBOutlet weak var faqBtn: UIButton!
    @IBOutlet weak var wokaSupportBtn: UIButton!
    @IBOutlet weak var profileBtn: UIButton!
    @IBOutlet weak var myOrderBtn: UIButton!
    @IBOutlet weak var addChildBtn: UIButton!
    @IBOutlet weak var deactivateBtn: UIButton!
    @IBOutlet weak var termsConditionBtn: UIButton!
    @IBOutlet weak var privacyPolicyBtn: UIButton!
    @IBOutlet weak var disclaimerBtn: UIButton!
    @IBOutlet weak var appVersion: UILabel!
    
    @IBOutlet weak var avatarImage: UIImageView!
    @IBOutlet weak var scrollView: UIScrollView!
    
    var vm = SideMenuVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
    }
    
    override func viewDidLayoutSubviews() {
        vm.themeSelect()
    }

    @IBAction func closeBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        self.sideMenuController?.hideMenu()
    }
    
    @IBAction func btnTapped(_ sender : UIButton){
        PersistentStorage.shared.addOthersCount()
        switch sender{
        case aboutBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.about])
        case faqBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.faq])
        case wokaSupportBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.support])
        case profileBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.profile])
        case myOrderBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.order])
        case addChildBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.addChild])
        case deactivateBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.deactivate])
        case termsConditionBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.termsCondition])
        case privacyPolicyBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.privacyPolicy])
        case disclaimerBtn:
            self.sideMenuController?.hideMenu()
            NotificationCenter.default.post(name: .linkPush, object: nil, userInfo: ["type": LinkTypeEnum.disclaimer])
        default:
            break
        }
    }
    
    @IBAction func logoutBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addOthersCount()
        
        // if its guest user navigate him to login
        if AuthFunc.shareInstance.getUserType() == 3{
            self.sideMenuController?.hideMenu()
            DispatchQueue.main.async {
                UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB))
            }
            return
        }
        
        let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil)
        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.yesNoAlertVC) as! YesNoAlertVC
        vcPush.mainTitleText = "WOKA"
        vcPush.contentLabel = "Do you want to LOGOUT from the WOKA APP".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
        vcPush.onDoneBlock = { [weak self] mode in
            guard let self else{return}
            switch mode{
            case .yes:
                PersistentStorage.shared.addOthersCount()
                //If user clicked to proceed on login. Call the api.
                vm.logoutUser { isDone in
                    self.sideMenuController?.hideMenu()
                    AuthFunc.shareInstance.logout()
                    UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB))
                }
            case .no:
                PersistentStorage.shared.addOthersCount()
                print("no")
            }
        }
        vcPush.modalPresentationStyle = .overCurrentContext
        vcPush.modalTransitionStyle = .crossDissolve
        self.present(vcPush, animated: true)
    }
    
    @IBAction func languageSwitchSlide(_ sender: CustomizableSegmentControl) {
        PersistentStorage.shared.addOthersCount()

        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success)
        Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { _ in
            switch sender.selectedSegmentIndex{
            case 0: // English
                AuthFunc.shareInstance.setDefaultLanguage(language: .english)
            case 1: // Hindi
                AuthFunc.shareInstance.setDefaultLanguage(language: .hindi)
            default:
                break
            }
            self.sideMenuController?.hideMenu()
        }
    }
}
