//
//  ProfileViewController.swift
//  Em-call
//
//  Created by Alex Sh on 01.03.2022.
//

import UIKit

class ProfileViewController: UIViewController,UITableViewDataSource, UITableViewDelegate,
                             UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
    let cellReuseIdentifier = "cell"
    var name = "Any name"
    var email = "e_mail@server.en"
    var contact = "+1 123 112 11 22"
    var card = "**** **** **** 1234"
    let imagePickerController = UIImagePickerController()
    
    @IBOutlet weak var roundImage: UIImageView!
   
   // @IBOutlet weak var tableview: UITableView!
    
    @IBOutlet weak var tableview: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = true
        tableview.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
        tableview.estimatedRowHeight = 44
        tableview.rowHeight = UITableView.automaticDimension
 
        tableview.delegate = self
        tableview.dataSource = self
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
        self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.black]
        self.navigationController?.navigationBar.barTintColor = UIColor.white
        self.navigationController?.navigationBar.topItem?.title = "PROFILE"
        self.navigationController?.navigationItem.title = "PROFILE"
    }
    
    @IBAction func pressChangePsw(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
         var vc:UIViewController
         vc = storyboard.instantiateViewController(withIdentifier: "ChangePswd")
         navigationController?.pushViewController(vc, animated: true)
    }
    
    @IBAction func pressImagePicker(_ sender: Any) {
        self.imagePickerController.sourceType = .savedPhotosAlbum // options: .Camera , .PhotoLibrary , .SavedPhotosAlbum
        self.present(self.imagePickerController, animated: true, completion: nil)
    }
    //The delegate UIImagePickerController
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage
        else {
            return }
        roundImage.image = image
        roundImage.layer.cornerRadius = 30
       // let imageName = UUID().uuidString
       // let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)

       // if let jpegData = image.jpegData(compressionQuality: 0.8) {
       //     try? jpegData.write(to: imagePath)
       // }

        dismiss(animated: true)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }

    func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return paths[0]
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
       return 4
   }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:            return 1
        case 1:            return 1
        case 2:            return 1
        case 3:            return 1
 
       // case 5:            return 2
        default:            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       // let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
        var cell:CustCellTableViewCell
        let section = indexPath.section
        switch section {
        case 0:
          cell = tableView.dequeueReusableCell(withIdentifier: "cust_cell2") as! CustCellTableViewCell
          cell.custCell.text = name
        case 1:
           cell = tableView.dequeueReusableCell(withIdentifier: "cust_cell2") as! CustCellTableViewCell
          cell.custCell2.text = email                 // cell.imageCell.isHidden = true
        case 2:
           cell = tableView.dequeueReusableCell(withIdentifier: "cust_cell2") as! CustCellTableViewCell
          cell.custCell2?.text = contact
          cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
        case 3:
           cell = tableView.dequeueReusableCell(withIdentifier: "cust_cell") as! CustCellTableViewCell
          cell.custCell?.text = card
                  cell.imageCell?.image = UIImage(named: "Vector")
          cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
       // default:           break
        default:
          cell = tableView.dequeueReusableCell(withIdentifier: "cust_cell2") as! CustCellTableViewCell
         cell.custCell2?.text = contact
          cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
        }
         cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
          return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
       //   let rets:String? = mainTable.tableView(tableView, titleForHeaderInSection: section)
      //   return rets!
        switch section {
 
         case 0:            return ("Name")
         case 1:            return ("Your e-mail")
         case 2:            return ("Your contacts")
         case 3:            return ("Payment")  
         default:            return ("Work location")
         }     // return ("Work location")
     }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var vc:UIViewController
        vc = storyboard.instantiateViewController(withIdentifier: "DataProfile")
        (vc as! DataProfileViewController).delegate = self
 
        navigationController?.pushViewController(vc, animated: true)
 
    }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        // Создаём константу, именна через неё мы будем обращаться к свойствам и изменять их
    let header = view as! UITableViewHeaderFooterView
        // Установить цвет текста в label
    header.textLabel?.textColor = .black
        // Установить цвет фона для секции
    header.tintColor = UIColor.white
        // Установить шрифт и размер шрифта для label
   // header.textLabel?.font = UIFont(name: "Helvetica-Regular", size: 12)
    }
}

/*extension ProfileViewController:MapViewControllerDelegate {
    func delegateClick(text:String) {
        address = text
     //   NotificationCenter.default.post(name: NSNotification.Name(rawValue: "delegateClick"), object: nil)
        tableView.reloadData()
    }
}*/
extension ProfileViewController:DataProfileViewControllerDelegate {
    func dataProfileDelegateClick(text:String,flagData:Int) {
 
        switch flagData {
        case 0:
            name = text
        case 1:
            email = text
        case 2:
            contact = text
        case 3:
            card = text
        default:
            break
        }
        tableview.reloadData()
    }
    
}
