//
//  PhotoEditingStickerToolVC.swift
//  WoWonder
//
//  Created by iMac on 23/05/25.
//  Copyright © 2025 ScriptSun. All rights reserved.
//

import UIKit
import FittedSheets

protocol PhotoEditingStickerToolVCDelegate {
    func handleStickerSelection(data: PhotoStickerModel)
    func handleStickerSelection(data: String)
    func handleStickerSelection(data: PhotoWidgetModel)
}

class PhotoEditingStickerToolVC: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var stickerButton: UIButton!
    @IBOutlet weak var emojisButton: UIButton!
    @IBOutlet weak var widgetButton: UIButton!
    @IBOutlet weak var containerView: UIView!
    
    // MARK: - Properties
    
    var pageMenu: CAPSPageMenu!
    var delegate: PhotoEditingStickerToolVCDelegate?
    
    // MARK: - View Life Cycles
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Close Button Action
    @IBAction func closeButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.sheetViewController?.attemptDismiss(animated: true)
    }
    
    // Sticker Button Action
    @IBAction func stickerButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.pageMenu.moveToPage(0)
    }
    
    // Emojis Button Action
    @IBAction func emojisButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.pageMenu.moveToPage(1)
    }
    
    // Widget Button Action
    @IBAction func widgetButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.pageMenu.moveToPage(2)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.setupPageMenu()
    }
    
    // Setup Page Menu
    func setupPageMenu() {
        var controllerArray: [UIViewController] = []
        let stickerVC: PhotoEditorStickerVC = PhotoEditorStickerVC.stickerVC()
        stickerVC.parentContorller = self
        stickerVC.delegate = self
        controllerArray.append(stickerVC)
        let emojisVC: PhotoEditorEmojisVC = PhotoEditorEmojisVC.emojisVC()
        emojisVC.parentContorller = self
        emojisVC.delegate = self
        controllerArray.append(emojisVC)
        let widgetVC: PhotoEditorWidgetVC = PhotoEditorWidgetVC.widgetVC()
        widgetVC.parentContorller = self
        widgetVC.delegate = self
        controllerArray.append(widgetVC)
        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!)
    }

}

// MARK: - Extensions

// MARK: PhotoEditorStickerVCDelegate
extension PhotoEditingStickerToolVC: PhotoEditorStickerVCDelegate {
    
    func handleStickerSelection(data: PhotoStickerModel) {
        self.delegate?.handleStickerSelection(data: data)
    }
    
}

// MARK: PhotoEditorEmojisVCDelegate
extension PhotoEditingStickerToolVC: PhotoEditorEmojisVCDelegate {
    
    func handleStickerSelection(data: String) {
        self.delegate?.handleStickerSelection(data: data)
    }
    
}

// MARK: PhotoEditorWidgetVCDelegate
extension PhotoEditingStickerToolVC: PhotoEditorWidgetVCDelegate {
    
    func handleStickerSelection(data: PhotoWidgetModel) {
        self.delegate?.handleStickerSelection(data: data)
    }
    
}

// MARK: CAPSPageMenuDelegate Methods
extension PhotoEditingStickerToolVC: CAPSPageMenuDelegate {
    
    func willMoveToPage(_ controller: UIViewController, index: Int) {
        switch index {
        case 0:
            self.stickerButton.setTitleColor(.accent, for: .normal)
            self.emojisButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.widgetButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
        case 1:
            self.stickerButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.emojisButton.setTitleColor(.accent, for: .normal)
            self.widgetButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
        case 2:
            self.stickerButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.emojisButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.widgetButton.setTitleColor(.accent, for: .normal)
        default:
            break
        }
    }
    
    func didMoveToPage(_ controller: UIViewController, index: Int) {
        switch index {
        case 0:
            self.stickerButton.setTitleColor(.accent, for: .normal)
            self.emojisButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.widgetButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
        case 1:
            self.stickerButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.emojisButton.setTitleColor(.accent, for: .normal)
            self.widgetButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
        case 2:
            self.stickerButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.emojisButton.setTitleColor(UIColor(hex: "808080"), for: .normal)
            self.widgetButton.setTitleColor(.accent, for: .normal)
        default:
            break
        }
    }
    
}
