//
//  QRCodeVC.swift
//  WoWonder
//
//  Created by iMac on 25/05/24.
//  Copyright © 2024 ScriptSun. All rights reserved.
//

import UIKit

class QRCodeVC: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var myCodeLabel: UILabel!
    @IBOutlet weak var myCodeBorder: UIView!
    @IBOutlet weak var scanCodeLabel: UILabel!
    @IBOutlet weak var scanCodeBorder: UIView!
    @IBOutlet weak var containerView: UIView!
    
    // MARK: - Properties
    
    var pageMenu: CAPSPageMenu!
    
    // MARK: - View Life Cycles

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.navigationController?.popViewController(animated: true)
    }
    
    // My Code Button Action
    @IBAction func myCodeButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.myCodeLabel.textColor = .accent
        self.myCodeBorder.backgroundColor = .accent
        self.scanCodeLabel.textColor = .text_color_secondary
        self.scanCodeBorder.backgroundColor = .clear
        self.pageMenu.moveToPage(0)
    }
    
    // Scan Code Button Action
    @IBAction func scanCodeButtonAction(_ sender: UIButton) {
        self.myCodeLabel.textColor = .text_color_secondary
        self.myCodeBorder.backgroundColor = .clear
        self.scanCodeLabel.textColor = .accent
        self.scanCodeBorder.backgroundColor = .accent
        self.pageMenu.moveToPage(1)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.setupPageMenu()
    }
    
    // Setup Page Menu
    func setupPageMenu() {
        var controllerArray: [UIViewController] = []
        let myCodeVC: MyCodeVC = MyCodeVC.myCodeVC()
        myCodeVC.parentContorller = self
        controllerArray.append(myCodeVC)
        let scanCodeVC: ScanCodeVC = ScanCodeVC.scanCodeVC()
        scanCodeVC.parentContorller = self
        controllerArray.append(scanCodeVC)
        let parameters: [CAPSPageMenuOption] = [
            .menuHeight(00)
        ]
        self.pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y: 0.0, width: self.containerView.bounds.width, height: self.containerView.bounds.height), pageMenuOptions: parameters)
        self.pageMenu.delegate = self
        self.containerView.addSubview(self.pageMenu.view!)
        DispatchQueue.main.async {
            self.pageMenu.moveToPage(1)
        }
    }
    
}

// MARK: - Extensions

// MARK: CAPSPageMenuDelegate Methods
extension QRCodeVC: CAPSPageMenuDelegate {
    
    func willMoveToPage(_ controller: UIViewController, index: Int) {
        switch index {
        case 0:
            self.myCodeLabel.textColor = .accent
            self.myCodeBorder.backgroundColor = .accent
            self.scanCodeLabel.textColor = .text_color_secondary
            self.scanCodeBorder.backgroundColor = .clear
        case 1:
            self.myCodeLabel.textColor = .text_color_secondary
            self.myCodeBorder.backgroundColor = .clear
            self.scanCodeLabel.textColor = .accent
            self.scanCodeBorder.backgroundColor = .accent
        default:
            break
        }
    }
    
    func didMoveToPage(_ controller: UIViewController, index: Int) {
        switch index {
        case 0:
            self.myCodeLabel.textColor = .accent
            self.myCodeBorder.backgroundColor = .accent
            self.scanCodeLabel.textColor = .text_color_secondary
            self.scanCodeBorder.backgroundColor = .clear
        case 1:
            self.myCodeLabel.textColor = .text_color_secondary
            self.myCodeBorder.backgroundColor = .clear
            self.scanCodeLabel.textColor = .accent
            self.scanCodeBorder.backgroundColor = .accent
        default:
            break
        }
    }
    
}
