//
//  SignaturesViewController.swift
//  iPOS
//
//  Created by Daniel Ciuciu on 11/18/19.
//  Copyright © 2019 BBC. All rights reserved.
//

import Foundation
import UIKit

class AuthorizationRequestsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    @IBOutlet weak var tableView: UITableView!
    var signatureImage: UIImage? = nil
    var refreshButton: UIBarButtonItem = UIBarButtonItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = Util.getString("Authorizations")
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.setNavigationBarItem()
        self.initData()
        self.createButton()
    }
    
    func createButton(){
        self.refreshButton = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(initData))
        self.navigationItem.rightBarButtonItem = self.refreshButton
    }
    
    @objc func initData(){
        State.LoadAuthorizationRequests(showActivityView: true) {
            self.tableView.reloadData()
        }
    }
        
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return State.authorizationRequests.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
        let authorization = State.authorizationRequests[indexPath.row]
        cell.textLabel?.text = authorization.reason ?? "Privacy"
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let authorizationRequest = State.authorizationRequests[indexPath.row]
        self.goToAuthorizationDetail(selectedAuthorizationRequest: authorizationRequest)
    }
    
    func goToAuthorizationDetail(selectedAuthorizationRequest: AuthorizationRequest){
        let detail = self.storyboard?.instantiateViewController(withIdentifier: "authorizationDetailViewController") as! AuthorizationDetailViewController
        detail.authorizationRequest = selectedAuthorizationRequest
        self.navigationController?.pushViewController(detail, animated: true)
    }
}

extension AuthorizationRequestsViewController : SlideMenuControllerDelegate {
   
    func leftWillOpen() {
    }
    
    func leftDidOpen() {
    }
    
    func leftWillClose() {
    }
    
    func leftDidClose() {
    }
    
    func rightWillOpen() {
    }
    
    func rightDidOpen() {
    }
    
    func rightWillClose() {
    }
    
    func rightDidClose() {
    }
}
