//
//  RightVideoTableViewCell.swift
//  WoWonder
//
//  Created by Mac on 10/11/22.
//  Copyright © 2022 ScriptSun. All rights reserved.
//

import UIKit

class RightVideoTableViewCell: SwipeToReplyCell {

    @IBOutlet weak var cellView: UIView!
    @IBOutlet weak var messageView: UIView!
    @IBOutlet weak var forwardedView: UIView!
    @IBOutlet weak var replyStoryView: UIView!
    @IBOutlet weak var replyStoryBorderView: UIView!
    @IBOutlet weak var replyStoryTitleLabel: UILabel!
    @IBOutlet weak var replyStoryMessageLabel: UILabel!
    @IBOutlet weak var replyStoryImageView: UIImageView!
    @IBOutlet weak var reactionImageView: UIImageView!
    @IBOutlet weak var replyMessageView: UIView!
    @IBOutlet weak var replyUserLabel: UILabel!
    @IBOutlet weak var replyMessageLabel: UILabel!
    @IBOutlet weak var messageVideoView: UIView!
    @IBOutlet weak var messageVideoButton: UIButton!
    @IBOutlet weak var messageTimeLabel: UILabel!
    @IBOutlet weak var playVideoButton: UIButton!
    @IBOutlet weak var videoImageView: UIImageView!
    @IBOutlet weak var starImageView: UIImageView!
    @IBOutlet weak var lastSeenImage: UIImageView!
    @IBOutlet weak var blurView: UIVisualEffectView!
    @IBOutlet weak var downloadView: UIView!
    @IBOutlet weak var downloadButton: UIButton!
    
    var progressView: UIProgressView!
    var videoUrl: URL?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // Initialization code
        
        self.messageView.clipsToBounds = true
        self.messageView.layer.cornerRadius = 16
        self.messageView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
        
        setupProgressView()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func downloadButtonAction(_ sender: UIButton) {
        guard let url = self.videoUrl else {
            appDelegate.window?.rootViewController?.view.makeToast("The video URL is not valid.")
            return
        }
        downloadVideo(from: url)
    }
    
    func setupProgressView() {
        progressView = UIProgressView(progressViewStyle: .default)
        progressView.center = self.messageVideoView.center
        self.messageVideoView.insertSubview(self.progressView, aboveSubview: self.blurView)
        progressView.isHidden = true
    }
    
    func downloadVideo(from url: URL) {
        self.downloadView.isHidden = true
        progressView.isHidden = false
        progressView.progress = 0.0
        
        MediaDownloader.shared.downloadFile(from: url, progressHandler: { progress in
            DispatchQueue.main.async {
                self.progressView.progress = progress
            }
        }) { [weak self] result in
            guard let self = self else { return }
            
            DispatchQueue.main.async {
                self.progressView.isHidden = true
            }
            
            switch result {
            case .success(let localPath):
                AppInstance.instance.generateThumbnail(from: localPath) { image in
                    if let thumbnail = image {
                        self.videoImageView.image = thumbnail
                    }
                }
                MediaDownloader.shared.saveVideoToPhotos(videoURL: localPath) { success, error in
                    DispatchQueue.main.async {
                        if success {
                            self.blurView.isHidden = true
                            appDelegate.window?.rootViewController?.view.makeToast("Video saved to Photos.")
                        } else if let error = error {
                            appDelegate.window?.rootViewController?.view.makeToast(error.localizedDescription)
                        }
                    }
                }
            case .failure(let error):
                DispatchQueue.main.async {
                    appDelegate.window?.rootViewController?.view.makeToast(error.localizedDescription)
                }
            }
        }
    }
    
}
