//
//  MediaDownloadAlertVC.swift
//  WoWonder
//
//  Created by iMac on 11/09/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import UIKit

enum MediaDownloadAlertType {
    case Mobile_Data
    case WiFi_Data
}

protocol MediaDownloadAlertVCDelegate {
    func handleMediaDownloadSelection(alertType: MediaDownloadAlertType, mediaSelection: [String])
}

class MediaDownloadAlertVC: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var imageImageView: UIImageView!
    @IBOutlet weak var videoImageView: UIImageView!
    
    // MARK: - Properties
    
    var alertType: MediaDownloadAlertType = .Mobile_Data
    var titleText = ""
    var delegate: MediaDownloadAlertVCDelegate?
    var selectedArray: [String] = []
    
    // MARK: - View Life Cycles

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Image Button Action
    @IBAction func imageButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        if self.selectedArray.contains("Image") {
            if let index = self.selectedArray.firstIndex(of: "Image") {
                self.selectedArray.remove(at: index)
            }
        } else {
            self.selectedArray.append("Image")
        }
        self.setupUI()
    }
    
    // Video Button Action
    @IBAction func videoButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        if self.selectedArray.contains("Video") {
            if let index = self.selectedArray.firstIndex(of: "Video") {
                self.selectedArray.remove(at: index)
            }
        } else {
            self.selectedArray.append("Video")
        }
        self.setupUI()
    }
    
    // Close Button Action
    @IBAction func closeButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.dismiss(animated: true) {
            self.delegate?.handleMediaDownloadSelection(alertType: self.alertType, mediaSelection: self.selectedArray)
        }
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.view.backgroundColor = .black.withAlphaComponent(0.4)
        self.setTitle()
        self.setupUI()
    }
    
    // Set Title
    func setTitle() {
        self.titleLabel.text = self.titleText
    }

    // Setup UI
    func setupUI() {
        let imageImageName = self.selectedArray.contains("Image") ? "ic_check_red" : "ic_uncheck_red"
        self.imageImageView.image = UIImage(named: imageImageName)
        let videoImageName = self.selectedArray.contains("Video") ? "ic_check_red" : "ic_uncheck_red"
        self.videoImageView.image = UIImage(named: videoImageName)
    }
    
}
