//
//  PlumbersViewController.swift
//  Em-call
//
//  Created by Alex Sh on 16.02.2022.
//

import UIKit



class PlumbersViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate,  UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate
{
    var settingsLauncher:SettingsLauncher = SettingsLauncher()
    
    @IBOutlet weak var glidingView: GlidingCollection!
    fileprivate var collectionView: UICollectionView!
 
    var selRow:Int = 0

   // let imagePickerController = UIImagePickerController()
    let cellReuseIdentifier = "cell"
    var address = "Unknow address"
    var dateAndTime = "2022/02/23"
    var contact = "+1 123 112 11 22"
    var card = "**** **** **** 1234"
    fileprivate var images: [UIImage?] = []
   
    
    @IBOutlet weak var textViewDesc: UITextView!
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
      tableView.rowHeight = 65

       tableView.delegate = self
       tableView.dataSource = self
        textViewDesc.delegate = self
        settingsLauncher.delegate = self
        //glide setup
        setup()
    }
    
    @IBAction func pressAddPhoto(_ sender: Any) {
        guard let navVC = self.navigationController
        else { return }
        settingsLauncher.showSettings(navigVC: navVC,dispatcher:0)
     }
 
    @IBAction func BOOK_NOW_Click(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc:UIViewController
        if #available(iOS 13.0, *) {
             vc = storyboard.instantiateViewController(identifier: "BookingVK") as! VKBookingViewController
        } else {
            vc = storyboard.instantiateViewController(withIdentifier: "BookingVK")
        }
         navigationController?.pushViewController(vc, animated: true)
    }
    
     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textViewDesc.resignFirstResponder()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
       return 4
   }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
         switch section {
         case 0:            return ("Work location")
         case 1:            return ("Date and Time")
         case 2:            return ("Your contacts")
         case 3:            return ("Payment")
         default:            return ("Work location")
         }     // return ("Work location")
     }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
        //let track = trackList[indexPath.row]
        let section = indexPath.section
        switch section {
        case 0:            cell.textLabel?.text = address
        case 1:            cell.textLabel?.text = dateAndTime
        case 2:            cell.textLabel?.text = contact
        case 3:            cell.textLabel?.text = card
        default:           break
        }
         cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
          return cell
    }
        // method to run when table view cell is tapped
         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let nameXibName = ["Map","Calendar"]
            let section = indexPath.section
            var vc:UIViewController
            switch section        //if indexPath.row == 0
            {
            case 0:                 vc = storyboard.instantiateViewController(withIdentifier: nameXibName[0])
                    (vc as! MapViewController).delegate = self
                navigationController?.pushViewController(vc , animated: true)
                return
            case 1:                  vc = storyboard.instantiateViewController(withIdentifier: nameXibName[1])
                 (vc as! CalendarViewController).delegate = self
                navigationController?.pushViewController(vc, animated: true)
                return
            case 2:             vc = storyboard.instantiateViewController(withIdentifier: nameXibName[1])
             (vc as! CalendarViewController).delegate = self
                navigationController?.pushViewController(vc, animated: true)
            case 3:             vc = storyboard.instantiateViewController(withIdentifier: nameXibName[1])
              (vc as! CalendarViewController).delegate = self
                navigationController?.pushViewController(vc, animated: true)
            default:            return
            }
         }
}

extension PlumbersViewController:MapViewControllerDelegate {
    func delegateClick(text:String) {
        address = text
     //   NotificationCenter.default.post(name: NSNotification.Name(rawValue: "delegateClick"), object: nil)
        tableView.reloadData()
    }
}
extension PlumbersViewController:CalendarViewControllerDelegate {
    func calendarDelegateClick(text:String,flagData:Int) {
 
        switch flagData {
        case 0:
            dateAndTime = text
        case 1:
            contact = text
        case 2:
            card = text
        default:
            break
        }
        tableView.reloadData()
    }
}
//
extension PlumbersViewController:SettingsLauncherDelegate {
    func settingsDelegateClick(img: UIImage?,selRow:Int) {
        if img != nil {
            images.append(img!)
            collectionView.reloadData()
        }
        else {
            self.selRow = selRow
        }
   
    }
}
    
// MARK: - Setup
extension PlumbersViewController {
  
  func setup() {
    setupGlidingCollectionView()
   // loadImages()
  }
  
  private func setupGlidingCollectionView() {
    glidingView.dataSource = self
    
    let nib = UINib(nibName: "CollectionCell", bundle: nil)
    collectionView = glidingView.collectionView
    collectionView.register(nib, forCellWithReuseIdentifier: "Cell")
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.backgroundColor = glidingView.backgroundColor
  }

}

// MARK: - CollectionView 🎛
extension PlumbersViewController: UICollectionViewDataSource, UICollectionViewDelegate {
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   // let section = glidingView.expandedItemIndex
    return images.count
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? CollectionCell else { return UICollectionViewCell() }
    ////let section = glidingView.expandedItemIndex
    if images.count>0 {
    let image = images[indexPath.row]
   // let image = images[0]
    cell.imageView.image = image
    }
    cell.contentView.clipsToBounds = true
    
    let layer = cell.layer
    let config = GlidingConfig.shared
    layer.shadowOffset = config.cardShadowOffset
    layer.shadowColor = config.cardShadowColor.cgColor
    layer.shadowOpacity = config.cardShadowOpacity
    layer.shadowRadius = config.cardShadowRadius
    
    layer.shouldRasterize = true
    layer.rasterizationScale = UIScreen.main.scale
    
    return cell
  }
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let section = glidingView.expandedItemIndex
    let item = indexPath.item
    print("Selected item #\(item) in section #\(section)")
  }
  
}
// MARK: - Gliding Collection 🎢
extension PlumbersViewController: GlidingCollectionDatasource {
  
  func numberOfItems(in collection: GlidingCollection) -> Int {
   // return items.count
    return 0
  }
  
  func glidingCollection(_ collection: GlidingCollection, itemAtIndex index: Int) -> String {
   // return "– " + items[index]
    return "– "
  }
}
