//
//  StoryBottomViewController.swift
//  WoWonder
//
//  Created by UnikApp on 13/12/22.
//  Copyright © 2022 ScriptSun. All rights reserved.
//

import UIKit

protocol StoryBottomDelegate {
    func handleAddStoryOptionTap(index: Int, optionName: String)
}

class StoryBottomViewController: UIViewController {

    // MARK: - IBOutlets
    
    @IBOutlet weak var textIcon: UIImageView!
    @IBOutlet weak var textView: UIControl!
    @IBOutlet weak var galleryIcon: UIImageView!
    @IBOutlet weak var galleryView: UIControl!
    @IBOutlet weak var cameraIcon: UIImageView!
    @IBOutlet weak var cameraView: 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: StoryBottomDelegate?
    
    // 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
    
    // Text Button Action
    @IBAction func textButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleAddStoryOptionTap(index: 0, optionName: "Text")
        }
    }
    
    // Image Button Action
    @IBAction func imageButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleAddStoryOptionTap(index: 1, optionName: "Gallery")
        }
    }
    
    // Video Button Action
    @IBAction func videoButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleAddStoryOptionTap(index: 2, optionName: "Camera")
        }
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.sheetHeight = 144 + 24 + 32 + 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() {
        
        
    }
    
    // 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)
        }
    }

}
