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

import UIKit
import Combine

class WallpaperViewController: UIViewController {
    
    // MARK: - IBOutlets
    
    @IBOutlet weak var selectedImageView: UIImageView!
    @IBOutlet weak var selectedColorView: UIView!
    
    // MARK: - Properties
    
    private let imagePicker = UIImagePickerController()
    private let colorPicker = UIColorPickerViewController()
    var selectedColor = ""
    var selectedImageData: Data?
    
    // MARK: - View Life Cycles
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.navigationController?.popViewController(animated: true)
    }
    
    // Save Button Action
    @IBAction func saveButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        let userSave = UserDefaults.standard
        if self.selectedColor == "" {
            userSave.setValue(self.selectedImageData, forKey: "imgWallpaper")
            userSave.removeObject(forKey: "colorWallpaper")
        } else {
            userSave.setValue(self.selectedColor, forKey: "colorWallpaper")
            userSave.removeObject(forKey: "imgWallpaper")
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    // Image Gallery Button Action
    @IBAction func imageGalleryButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.imagePicker.delegate = self
        self.imagePicker.sourceType = .photoLibrary
        self.imagePicker.mediaTypes = ["public.image"]
        self.imagePicker.allowsEditing = false
        self.present(self.imagePicker, animated: true, completion: nil)
    }
    
    // Color Button Action
    @IBAction func colorButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        let picker = UIColorPickerViewController()
        picker.delegate = self
        picker.selectedColor = .accent
        picker.modalPresentationStyle = .overCurrentContext
        picker.modalTransitionStyle = .crossDissolve
        self.present(picker, animated: true, completion: nil)
    }
    
    // Default Wallpaper Button Action
    @IBAction func defaultWallpaperButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        let userSave = UserDefaults.standard
        userSave.removeObject(forKey: "colorWallpaper")
        userSave.removeObject(forKey: "imgWallpaper")
        self.selectedColorView.backgroundColor = .darkGray
        self.selectedColor = ""
        self.selectedColorView.isHidden = false
        self.selectedImageView.image = nil
        self.selectedImageData = nil
        self.selectedImageView.isHidden = true
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.setupUI()
    }
    
    // Setup UI
    func setupUI() {
        let userDefault = UserDefaults.standard
        if let colorWallpaper = userDefault.value(forKey: "colorWallpaper") as? String {
            self.selectedColorView.backgroundColor = UIColor(hex: colorWallpaper)
            self.selectedColorView.isHidden = false
            self.selectedImageView.isHidden = true
        }
        if let imgWallpaper = userDefault.value(forKey: "imgWallpaper") as? Data {
            self.selectedImageView.image = UIImage(data: imgWallpaper)
            self.selectedImageView.isHidden = false
            self.selectedColorView.isHidden = true
        }
    }
}

// MARK: - Extensions

//MARK: UIImagePickerController & UINavigationController Delegate Methods
extension WallpaperViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true) {
            if let image = info[.originalImage] as? UIImage {
                let imageData = image.jpegData(compressionQuality: 1)
                self.selectedImageView.image = image
                self.selectedImageData = imageData
                self.selectedImageView.isHidden = false
                self.selectedColorView.backgroundColor = .darkGray
                self.selectedColor = ""
                self.selectedColorView.isHidden = true
            }
        }
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
}

//MARK: UIColorPicker Delegate Methods
extension WallpaperViewController: UIColorPickerViewControllerDelegate {
    
    //  Called on every color selection done in the picker.
    func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
        viewController.dismiss(animated: true) {
            let selectedColor = viewController.selectedColor
            self.selectedColorView.backgroundColor = selectedColor
            self.selectedColor = selectedColor.toHexString()
            self.selectedColorView.isHidden = false
            self.selectedImageView.image = nil
            self.selectedImageData = nil
            self.selectedImageView.isHidden = true
        }
    }
    
    func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
        viewController.dismiss(animated: true, completion: nil)
    }
    
}
