//
//  DocumentsViewController.swift
//  iPOS
//
//  Created by Apple iMac on 12/07/2017.
//  Copyright © 2017 BBC. All rights reserved.
//

import Foundation

class DocumentsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var contact: Contact!
    var documents: [Document] = [Document]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource = self
        createButton()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if State.documentTypes == nil{
            loadDocumentTypes()
        }
        else{
            loadDocuments()
        }
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return false
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return documents.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! DocumentCustomCell
        let document: Document = documents[indexPath.row]
        cell.lfName.text = document.name
        cell.lfDate.text = Util.DateToShortDateString(date: document.createTime!)
        
        if let type = document.documentType{
            if type == 0 { 
                cell.lfType.text = Util.getString("Inv. no.") + "\(document.invoiceNumber)"
            }
            else{
                cell.lfType.text = document.documentTypeName
            }
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let document = documents[indexPath.row]
        let documentDetailViewContoller = (self.storyboard?.instantiateViewController(withIdentifier: "documentDetailViewController") as! DocumentDetailViewController)
        documentDetailViewContoller.contact = contact
        documentDetailViewContoller.documentId = document.documentId
        documentDetailViewContoller.viewTitle = document.name
        documentDetailViewContoller.documentTypeId = document.documentType
        documentDetailViewContoller.layoutId = document.layoutId
        documentDetailViewContoller.layoutType = document.layoutType
        self.navigationController?.pushViewController(documentDetailViewContoller, animated: true)
    }
    
    func createButton(){
        let addDocument = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject))
        self.navigationItem.rightBarButtonItem = addDocument
    }
    
    @objc func insertNewObject(){
        let addDocumentViewController = (self.storyboard?.instantiateViewController(withIdentifier: "addDocumentViewController") as! AddDocumentViewController)
        addDocumentViewController.contactId = contact.ContactId
        self.navigationController?.pushViewController(addDocumentViewController, animated: true)
    }
    
    func loadDocuments(){
        State.loadDocuments(contactId: contact.ContactId) { (result) in
            if result != nil{
                self.documents = result!
                self.tableView.reloadData()
            }
            else{
                Util.GeneralAlert(viewController: self, message: Util.getString("An error has occured.\nPlease try again!"))
            }
        }
    }
    
    func loadDocumentTypes(){
        State.loadDocumentTypes { (result) in
            if !result{
                Util.GeneralAlert(viewController: self, message: Util.getString("An error has occured.\nPlease try again!"))
            }
            else{
                self.loadDocuments()
            }
        }
    }
    
}
