//
//  PinnedAndFavouriteMessageOptionVC.swift
//  WoWonder
//
//  Created by iMac on 17/07/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import UIKit

protocol PinnedAndFavouriteMessageOptionVCDelegate {
    func handleMessageOptionTap(index: Int, optionName: String, indexPath: IndexPath)
}

class PinnedAndFavouriteMessageOptionVC: UIViewController {

    // MARK: - IBOutlets
        
    @IBOutlet weak var copyView: UIControl!
    @IBOutlet weak var messageInfoView: UIControl!
    @IBOutlet weak var deleteMessageView: UIControl!
    @IBOutlet weak var forwardView: UIControl!
    @IBOutlet weak var pinView: UIControl!
    @IBOutlet weak var pinTitleLabel: UILabel!
    @IBOutlet weak var favouriteView: UIControl!
    @IBOutlet weak var favouriteTitleLabel: UILabel!
    @IBOutlet weak var closeButton: UIButton!
    
    // MARK: - Properties
    
    var delegate: PinnedAndFavouriteMessageOptionVCDelegate?
    var isCopy = false
    var indexPath = IndexPath()
    var isRightUser = false
    var message: MessageModel?
    
    // 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.dismiss(animated: true)
    }
        
    // Copy Button Action
    @IBAction func copyButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMessageOptionTap(index: 0, optionName: "Copy", indexPath: self.indexPath)
        }
    }
    
    // Message Button Action
    @IBAction func messageInfoButtonAction(_ sender: UIColor) {
        self.dismiss(animated: true) {
            self.delegate?.handleMessageOptionTap(index: 1, optionName: "Message Info", indexPath: self.indexPath)
        }
    }
    
    // Delete Message Button Action
    @IBAction func deleteMessaageButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMessageOptionTap(index: 2, optionName: "Delete Message", indexPath: self.indexPath)
        }
    }
    
    // Forward Button Action
    @IBAction func forwardButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMessageOptionTap(index: 3, optionName: "Forward", indexPath: self.indexPath)
        }
    }
    
    // pin Button Action
    @IBAction func pinButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMessageOptionTap(index: 4, optionName: "Pin", indexPath: self.indexPath)
        }
    }
    
    // Favourite Button Action
    @IBAction func favouriteButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMessageOptionTap(index: 5, optionName: "Favourite", indexPath: self.indexPath)
        }
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.view.backgroundColor = .black.withAlphaComponent(0.4)
        self.setUpUI()
    }
    
    // SetUp UI
    func setUpUI() {
        self.copyView.isHidden = !self.isCopy
        self.messageInfoView.isHidden = !self.isRightUser
        self.deleteMessageView.isHidden = !self.isRightUser
        self.pinTitleLabel.text = (self.message?.pin == "yes") ? "Unpin" : "Pin"        
        self.favouriteTitleLabel.text = (self.message?.fav == "yes") ? "UnFavourite" : "Favourite"
        if let objColor = UserDefaults.standard.value(forKey: "ColorTheme") as? String, objColor != "" {
            self.closeButton.setTitleColor(UIColor(hex: objColor), for: .normal)
        }
    }
    
}
