//
//  EmerHandViewController.swift
//  Em-call
//
//  Created by Alex Sh on 17.02.2022.
//

import UIKit

class EmerHandViewController: UIViewController, UITableViewDataSource,
                              UITableViewDelegate
{
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var addressView: UIView!
    
   // 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"

    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Emergency handyman"
        navigationController?.navigationBar.titleTextAttributes = [.foregroundColor:UIColor(red: 255 / 255, green: 94 / 255, blue: 58 / 255, alpha: 1.0)]
 
        addressView.layer.borderColor =  UIColor.lightGray.cgColor
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
      tableView.rowHeight = 45
       tableView.delegate = self
       tableView.dataSource = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
        title = "Emergency handyman"
        navigationController?.navigationBar.titleTextAttributes = [.foregroundColor:UIColor(red: 255 / 255, green: 94 / 255, blue: 58 / 255, alpha: 1.0)]
 
         let item1 = setBarButtom(str: "akar-icons_arrow-left")
         self.navigationItem.setLeftBarButtonItems([item1], animated: true)

        let item2 = setBarButtom(str: "gg_close")
        self.navigationItem.setRightBarButtonItems([item2], animated: true)
      }
    
    func setBarButtom(str:String)->UIBarButtonItem {
        let btn1 = UIButton(type: .custom)
         btn1.setImage(UIImage(named: str), for: .normal)
         btn1.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
         btn1.addTarget(self, action: #selector(self.back), for: .touchUpInside)
         let item1 = UIBarButtonItem(customView: btn1)
        return item1
    }
    
    @objc
    func back() {
       //// self.dismiss(animated: true, completion: nil)
         navigationController?.popViewController( animated: true)
    }
    
    @IBAction func pressBookNow(_ 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?.isNavigationBarHidden = true
        navigationController?.pushViewController(vc, animated: true)
    }

    @IBAction func pressButMap(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let  vc = storyboard.instantiateViewController(withIdentifier: "Map")
        (vc as! MapViewController).delegate = self
        navigationController?.pushViewController(vc, animated: true)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
    }
    
     func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
   func tableView(_ tableView: UITableView, titleForHeaderInSection  section: Int) -> String? {
       switch section {
        case 0:            return ("Payment")
       /* case 1:            return ("Date and Time")        case 2:            return ("Your contacts")
        case 3:            return ("Payment")*/
        default:            return ("Payment")
        }
    }
    
    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 = card
      /*  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) {
         print("You tapped cell number \(indexPath.row).")
        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[1])
                 (vc as! CalendarViewController).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
        }
     }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        // Создаём константу, именна через неё мы будем обращаться к свойствам и изменять их
    let header = view as! UITableViewHeaderFooterView
        // Установить цвет текста в label
    header.textLabel?.textColor = .darkGray
        // Установить цвет фона для секции
    header.tintColor = UIColor.white
        // Установить шрифт и размер шрифта для label
    header.textLabel?.font = UIFont(name: "Helvetica-Regular", size: 15)
    }
}

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