//
//  BasePresentViewController.swift
//  ColorfulWallpaper
//
//  Created by nkl on 2024/9/25.
//

import Foundation
import UIKit

class BasePresentViewController: LWBaseViewController {
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.modalPresentationStyle = .overFullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let tapGesture = UITapGestureRecognizer()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = alertBackgroundColor
        
        if shouldDismissWhenTapBackground {
            tapGesture.addTarget(self, action: #selector(tapBackground))
            tapGesture.delegate = self
            view.addGestureRecognizer(tapGesture)
        }
    }
    
    @objc private func tapBackground() {
        dismissPresent()
    }
    
    var shouldDismissWhenTapBackground: Bool {
        return false
    }
    
    var alertBackgroundColor: UIColor {
        return UIColor.black.withAlphaComponent(0.4)
    }
    
    var animationContentView: UIView {
        return view
    }
    
    func dismissPresent() {
        
    }
}

extension BasePresentViewController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == tapGesture {
            let point = gestureRecognizer.location(in: view)
            if animationContentView.frame.contains(point) {
                return false
            }
        }
        return true
    }
}
