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

import UIKit


class PlumViewController: UIViewController, UITableViewDataSource,
                          UITableViewDelegate
 {
   // let trackList = Track.getTrackList()
    let cellReuseIdentifier = "cell"
    var address = "Unknow address"
    var dateAndTime = "2022/02/23"
    var contact = "+1 123 112 11 22"
    var card = "**** **** **** 1234"

    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
      tableView.rowHeight = 45
 
       tableView.delegate = self
       tableView.dataSource = self

    }

    @IBAction func BOOK_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)
    }
    
  //  @IBAction func tapGesture(_ sender: Any) {    view.endEditing(true)   }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       // return trackList.count
        return 1
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
       return 4
   }// Default is 1 if not implemented
   
    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 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
   // @available(iOS 13.0, *)
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         print("You tapped cell number \(indexPath.row).")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let nameXibName = ["Map","Calendar"]
      //  var indexPath = tableView.indexPathForSelectedRow!
       let vc:UIViewController
        switch indexPath.section        //if indexPath.row == 0
        {
        case 0:
                 vc = storyboard.instantiateViewController(withIdentifier: nameXibName[0])
            (vc as! MapViewController).delegate = self
       // navigationController?.pushViewController(vc, animated: true)
        case 1:
             vc = storyboard.instantiateViewController(withIdentifier: nameXibName[1])
            (vc as! CalendarViewController).delegate = self
          //  navigationController?.pushViewController(vc, animated: true)
        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
        }
        navigationController?.pushViewController(vc, animated: true)
     }


}

extension PlumViewController:MapViewControllerDelegate {
    func delegateClick(text:String) {
        address = text
     //   NotificationCenter.default.post(name: NSNotification.Name(rawValue: "delegateClick"), object: nil)
        tableView.reloadData()
    }
}
extension PlumViewController: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()
    }
    
}
