//
//  ShowImageController.swift
//  WoWonder
//
//  Created by Ubaid Javaid on 7/22/20.
//  Copyright © 2020 ScriptSun. All rights reserved.
//

import UIKit
import SDWebImage

class ShowImageController: UIViewController {

    //MARK: - IBOutlets
    
     @IBOutlet weak var showImageView: UIImageView!
    
    // MARK: - Properties
      
    var imageURL = ""
    var isGif = false
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        let url = URL(string: self.imageURL)
        if self.isGif {
            let indicator = SDWebImageActivityIndicator.medium
            self.showImageView.sd_imageIndicator = indicator
            DispatchQueue.global(qos: .userInteractive).async {
                self.showImageView.sd_setImage(with: url)
            }
        } else {
            let indicator = SDWebImageActivityIndicator.medium
            self.showImageView.sd_imageIndicator = indicator
            DispatchQueue.global(qos: .userInteractive).async {
                self.showImageView.sd_setImage(with: url, placeholderImage: UIImage(named: ""))
            }
        }
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    
    // Share Button Action
    @IBAction func shareButtonAction(_ sender: UIButton) {
        let textToShare = [ self.imageURL ]
        let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view
        activityViewController.excludedActivityTypes = [
            UIActivity.ActivityType.airDrop,
            UIActivity.ActivityType.postToFacebook,
            UIActivity.ActivityType.assignToContact,
            UIActivity.ActivityType.mail,
            UIActivity.ActivityType.postToTwitter,
            UIActivity.ActivityType.message,
            UIActivity.ActivityType.postToFlickr,
            UIActivity.ActivityType.postToVimeo,
            UIActivity.ActivityType.init(rawValue: "net.whatsapp.WhatsApp.ShareExtension"),
            UIActivity.ActivityType.init(rawValue: "com.google.Gmail.ShareExtension"),
            UIActivity.ActivityType.init(rawValue: "com.toyopagroup.picaboo.share"),
            UIActivity.ActivityType.init(rawValue: "com.tinyspeck.chatlyio.share")
        ]
        self.present(activityViewController, animated: true, completion: nil)
    }
    
}
