//
//  ChatMediaOptionVC.swift
//  WoWonder
//
//  Created by iMac on 26/06/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import UIKit

protocol ChatMediaOptionVCDelegate {
    func handleCameraButtonTap()
    func handleImageAndVideoButtonTap()
    func handleDocumentButtonTap()
    func handleContactButtonTap()
    func handleGifButtonTap()
    func handleStickerButtonTap()
    func handleMusicButtonTap()
    func handleLocationButtonTap()
}

class ChatMediaOptionVC: UIViewController {
    
    // 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: ChatMediaOptionVCDelegate?
    
    // 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
    
    // Camera Button Action
    @IBAction func cameraButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleCameraButtonTap()
        }
    }
    
    // Image And Video Button Action
    @IBAction func imageAndVideoButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleImageAndVideoButtonTap()
        }
    }
    
    // Document Button Action
    @IBAction func documentButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleDocumentButtonTap()
        }
    }
    
    // Contact Button Action
    @IBAction func contactButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleContactButtonTap()
        }
    }
    
    // Gif Button Action
    @IBAction func gifButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleGifButtonTap()
        }
    }
    
    // Sticker Button Action
    @IBAction func stickerButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleStickerButtonTap()
        }
    }
    
    // Music Button Action
    @IBAction func musicButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleMusicButtonTap()
        }
    }
    
    // Location Button Action
    @IBAction func locationButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleLocationButtonTap()
        }        
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.sheetHeight = 98 + self.view.safeAreaBottom
        view.frame.size.height = sheetHeight
        view.isUserInteractionEnabled = true
        view.backgroundColor = sheetBackgroundColor
        view.roundCorners(corners: [.topLeft, .topRight], radius: sheetCornerRadius)
        self.setPanGesture()
    }
    
    // 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)
        }
    }

}
