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

import UIKit

protocol GroupBottomMoreVCDelegate {
    func handleGroupMoreOptionTap(optionName: String, index: Int)
}

class GroupBottomMoreVC: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var addMembersIcon: UIImageView!
    @IBOutlet weak var addMembersView: UIControl!
    @IBOutlet weak var groupInfoIcon: UIImageView!
    @IBOutlet weak var groupInfoView: UIControl!
    @IBOutlet weak var exitGroupIcon: UIImageView!
    @IBOutlet weak var exitGroupView: 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: GroupBottomMoreVCDelegate?
    var groupData: GroupChat?
    
    // 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
    
    // Add Members Button Action
    @IBAction func addMembersButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleGroupMoreOptionTap(optionName: "Add Members", index: 0)
        }
    }
    
    // Group Info Button Action
    @IBAction func groupInfoButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleGroupMoreOptionTap(optionName: "Group Info", index: 1)
        }
    }
    
    // Exit group Button Action
    @IBAction func exitGroupButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleGroupMoreOptionTap(optionName: "Exit group", index: 2)
        }
    }
    
    // Search the conversation Button Action
    @IBAction func searchTheConversationButtonAction(_ sender: UIControl) {
        self.dismiss(animated: true) {
            self.delegate?.handleGroupMoreOptionTap(optionName: "Search the conversation", index: 3)
        }
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.sheetHeight = ((self.groupData?.owner ?? false) ? 192 : 144) + 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.addMembersView.isHidden = !(self.groupData?.owner ?? false)
    }
    
    // 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)
        }
    }
    
}
