//
//  AddOrderReviewVC.swift
//  Garmentoo
//
//  Created by MAC on 11/04/22.
//

import UIKit

class AddOrderReviewVC: UIViewController, UITextViewDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBOutlet weak var reviewBox:UITextView!

    @IBOutlet weak var imageCollectionView: UICollectionView!
    @IBOutlet weak var img: UIImageView!
    
    @IBOutlet weak var previewView: UIView!
    @IBOutlet weak var previewViewHeight: NSLayoutConstraint!
    var images = [UIImage]()
    
    var picker = UIImagePickerController()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.reviewBox.text = "Write an enquiry"
        self.reviewBox.textColor = UIColor.lightGray
        ShowPreviewView()
        
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(longPressGesture:)))
            longPressGesture.minimumPressDuration = 0.5
            self.imageCollectionView.addGestureRecognizer(longPressGesture)
    }
    func textViewDidBeginEditing(_ textView: UITextView)
    {
         if textView.textColor == UIColor.lightGray {
               textView.text = nil
               textView.textColor = UIColor.black
           }
        
    }
    @IBAction func onTapBackBtn(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }

    
    @IBAction func OnTapAddImage(_ sender: Any) {
        addImage()
    }
    
    func ShowPreviewView()
    {
        if (images.count == 0) {
            self.previewView.isHidden = true
            self.previewViewHeight.constant = 0
        }
        else
        {
            self.previewView.isHidden = false
            self.previewViewHeight.constant = 100
        }
    }
    
}

extension AddOrderReviewVC:UICollectionViewDelegate,UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imagecell", for: indexPath) as! ImageCollectionViewCell
        cell.imgView.image = images[indexPath.row]
       
        
        return cell
    }
    
    
}

extension AddOrderReviewVC
{
   func addImage()
   {
    let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertController.Style.actionSheet)
    let cameraAction = UIAlertAction(title: "Camera", style: .default)
           {
               UIAlertAction in
               self.openCamera()
       }
    let gallaryAction = UIAlertAction(title: "Gallery", style: .default)
           {
               UIAlertAction in
               self.openGallery()
       }
       let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
           {
               UIAlertAction in
       }

       // Add the actions
       picker.delegate = self
       alert.addAction(cameraAction)
       alert.addAction(gallaryAction)
       alert.addAction(cancelAction)
    self.present(alert, animated: true, completion: nil)
   }
   func openCamera(){
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)){
        picker.sourceType = UIImagePickerController.SourceType.camera
        self .present(picker, animated: true, completion: nil)
       }else{
           let alert = UIAlertView()
           alert.title = "Warning"
           alert.message = "You don't have camera"
        alert.addButton(withTitle: "OK")
           alert.show()
       }
   }
   func openGallery(){
    picker.sourceType = UIImagePickerController.SourceType.photoLibrary
    self.present(picker, animated: true, completion: nil)
   }
    
    @objc func longPressed(longPressGesture: UILongPressGestureRecognizer)
    {
        let p = longPressGesture.location(in: self.imageCollectionView)
           let indexPath = self.imageCollectionView.indexPathForItem(at: p)
           if indexPath == nil {
               print("Long press on table view, not row.")
           } else if longPressGesture.state == UIGestureRecognizer.State.began {
            let alert = UIAlertController(title: "Alert", message: "Are you sure you want to delete the selected image?", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
                self.images.remove(at: indexPath!.row)
                self.ShowPreviewView()
                self.imageCollectionView.reloadData()
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            alert.addAction(okAction)
            alert.addAction(cancelAction)
            self.present(alert, animated: true, completion: nil)
            
           }
    }
    
   //MARK:UIImagePickerControllerDelegate
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
           picker.dismiss(animated: true, completion: nil)
           if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            self.images.append(image)
           // self.img.image = images[0]
            self.ShowPreviewView()
            self.imageCollectionView.reloadData()
           }
    }
   func imagePickerControllerDidCancel(picker: UIImagePickerController){
       print("picker cancel.")
   }
}
