//
//  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!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = true
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
      tableView.rowHeight = 45
 
       tableView.delegate = self
       tableView.dataSource = self
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        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)
        //let track = trackList[indexPath.row]
        let section = indexPath.section
        switch section {
      //  case 0: cell.textLabel?.text = ""
      //      return cell
        case 0:            cell.textLabel?.text = name
        case 1:            cell.textLabel?.text = email
        case 2:            cell.textLabel?.text = contact
        case 3:            cell.textLabel?.text = card
        default:           break
        }
         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 ("")
           // return ("ico")
         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
      /// let nameXibName = ["Map","Calendar"]
      // let section = indexPath.section
        navigationController?.pushViewController(vc, animated: true)
     /*  switch section        //if indexPath.row == 0
       {
        case 0:
           navigationController?.pushViewController(vc, animated: true)
           return
       case 1:
           navigationController?.pushViewController(vc, animated: true)
       case 2:
           navigationController?.pushViewController(vc, animated: true)
       case 3:
           navigationController?.pushViewController(vc, animated: true)
       default:            return
       }*/
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

/*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()
    }
    
}
