//
//  MoreOptionViewController.swift
//  WoWonder
//
//  Created by Mac on 17/11/22.
//  Copyright © 2022 ScriptSun. All rights reserved.
//

import UIKit

protocol MoreOptionDelegate {
    func handleMoreOptionTap(optionName: String, index: Int)
}

class MoreOptionViewController: UIViewController {
    
    // MARK: - IBOutlets

    @IBOutlet weak var viewProfileIcon: UIImageView!
    @IBOutlet weak var viewProfileView: UIControl!
    @IBOutlet weak var blockIcon: UIImageView!
    @IBOutlet weak var blockTitleLabel: UILabel!
    @IBOutlet weak var blockView: UIControl!
    @IBOutlet weak var changeChatThemeIcon: UIImageView!
    @IBOutlet weak var changeChatThemeView: UIControl!
    @IBOutlet weak var wallpaperIcon: UIImageView!
    @IBOutlet weak var wallpaperView: UIControl!
    @IBOutlet weak var clearChatIcon: UIImageView!
    @IBOutlet weak var clearChatView: UIControl!
    @IBOutlet weak var favouritedMessagesIcon: UIImageView!
    @IBOutlet weak var favouritedMessagesView: UIControl!
    @IBOutlet weak var mediaIcon: UIImageView!
    @IBOutlet weak var mediaView: UIControl!
    @IBOutlet weak var searchTheConversationIcon: UIImageView!
    @IBOutlet weak var searchTheConversationView: UIControl!
    
    // MARK: - Properties
    
    var sheetHeight: CGFloat = 0
    var sheetBackgroundColor: UIColor = .color_fill
    var sheetCornerRadius: CGFloat = 20
    private var hasSetOriginPoint = false
    private var originPoint: CGPoint?
    var delegate: MoreOptionDelegate?
    var reciverUserData: UserData?
    var isFavourite = false
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    override func viewDidLayoutSubviews() {
        if !hasSetOriginPoint {
            hasSetOriginPoint = true
            originPoint = view.frame.origin
        }
    }
    
    // MARK: - Selectors
    
    // Copy Button Action
    @IBAction func viewProfileButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "View Profile", index: 0)
        }
    }
    
    // Message Button Action
    @IBAction func blockButtonAction(_ sender: UIColor) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "Block", index: 1)
        }
    }
    
    // Delete Message Button Action
    @IBAction func changeChatThemeButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "Change Chat Theme", index: 2)
        }
    }
    
    // Reply Button Action
    @IBAction func wallpaperButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "Wallpaper", index: 3)
        }
    }
    
    // Forward Button Action
    @IBAction func clearChatButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "Clear Chat", index: 4)
        }
    }
    
    // Pin Button Action
    @IBAction func favouritedMessagesButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "Favourited Messages", index: 5)
        }
    }
    
    // Favourite Button Action
    @IBAction func mediaButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "Media", index: 6)
        }
    }
    
    // Search the conversation Button Action
    @IBAction func searchTheConversationButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMoreOptionTap(optionName: "Search the conversation", index: 7)
        }
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.sheetHeight = (self.isFavourite ? 384 : 336) + 32 + self.view.safeAreaBottom
        view.frame.size.height = sheetHeight
        view.isUserInteractionEnabled = true
        view.backgroundColor = sheetBackgroundColor
        view.roundCorners(corners: [.topLeft, .topRight], radius: sheetCornerRadius)
        self.setPanGesture()
        self.setUpUI()
    }
    
    // SetUp UI
    func setUpUI() {
        self.favouritedMessagesView.isHidden = !self.isFavourite
        self.blockTitleLabel.text = (reciverUserData?.is_blocked ?? false) ? "UnBlock" : "Block"
        if let objColor = UserDefaults.standard.value(forKey: "ColorTheme") as? String, objColor != "" {
            self.viewProfileIcon.tintColor = UIColor(hex: objColor)
            self.blockIcon.tintColor = UIColor(hex: objColor)
            self.changeChatThemeIcon.tintColor = UIColor(hex: objColor)
            self.wallpaperIcon.tintColor = UIColor(hex: objColor)
            self.clearChatIcon.tintColor = UIColor(hex: objColor)
            self.favouritedMessagesIcon.tintColor = UIColor(hex: objColor)
            self.mediaIcon.tintColor = UIColor(hex: objColor)
            self.searchTheConversationIcon.tintColor = UIColor(hex: objColor)
        }
    }
    
    // Set Pan Gesture
    func setPanGesture() {
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerAction))
        view.addGestureRecognizer(panGesture)
    }
    
    // Gesture Recognizer
    @objc func panGestureRecognizerAction(sender: UIPanGestureRecognizer) {
        let translation = sender.translation(in: view)
        // Not allowing the user to drag the view upward
        guard translation.y >= 0 else { return }
        view.frame.origin = CGPoint(
            x: 0,
            y: self.originPoint!.y + translation.y
        )
        if sender.state == .ended {
            self.dismiss(animated: true, completion: nil)
        }
    }
    
}
