//
//  ProductContainersViewController.swift
//  iPOS
//
//  Created by Zoran Albu on 8/2/19.
//  Copyright © 2019 BBC. All rights reserved.
//

import Foundation
import UIKit

class ProductContainersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    @IBOutlet weak var tableView: UITableView!
    var productContainers: [ProductContainer] = []
    
    var add: UIBarButtonItem = UIBarButtonItem()
    var export: UIBarButtonItem = UIBarButtonItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = Util.getString("Product Containers")
        
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.setNavigationBarItem()
        self.createRightButtons()
        self.loadProductContainers()
    }
    
    func loadProductContainers(){
        State.loadProductContainers { result in
            if let productContainers = result{
                self.productContainers = productContainers
                self.tableView.reloadData()
            }
            else{
                NetworkController.AlertConnectionError(viewController: self)
                self.removeRightButtons()
            }
        }
    }
    
    func createRightButtons(){
        self.add = UIBarButtonItem(barButtonSystemItem: .add , target: self, action: #selector(addItem))
        self.navigationItem.rightBarButtonItems = [self.editButtonItem, add]
    }
    
    func removeRightButtons(){
        self.navigationItem.rightBarButtonItems = []
    }
    
    @objc func addItem(){
        Util.simpleAlertWithTextEdit(title: "", message: Util.getString("Insert the file name"), UIViewController: self, actionYes: { result in
            if let filename = result{
                let productContainer = ProductContainer()
                productContainer.productContainerName = filename
                self.goToProductContainerDetails(productContainer: productContainer, isNew: true)
            }
        }) {
            return
        }
    }
    
    func goToProductContainerDetails(productContainer: ProductContainer, isNew: Bool){
        let productContainerDetailsViewController = (self.storyboard?.instantiateViewController(withIdentifier: "productContainerDetailsViewController") as! ProductContainerDetailsViewController)
        productContainerDetailsViewController.productContainer = productContainer
        self.navigationController?.pushViewController(productContainerDetailsViewController, animated: true)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.productContainers.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let productContainer = self.productContainers[indexPath.row]
        self.goToProductContainerDetails(productContainer: productContainer, isNew: false)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ProductContainerCell
        
        let productContainer = self.productContainers[indexPath.row]
        cell.lfFileName.text = productContainer.productContainerName
        cell.lfFileName.font = UIFont.boldSystemFont(ofSize: 17.0)
        cell.lfDate.text = Util.DateToMaterialDesignString(date: productContainer.createTime)
        
        cell.lfFileStatus.textColor = UIColor.green
        cell.lfFileStatus.text = Util.getString("Exported")
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if self.isEditing{
            return true
        }
        return false
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let productContainer = self.productContainers[indexPath.row]
            State.deleteProductContainers(productContainer) { result in
                if let productContainerResult = result{
                    if let msg = productContainerResult.errorMessage{
                        Util.GeneralAlert(viewController: self, message: msg)
                    }
                    else{
                        self.productContainers.remove(at: indexPath.row)
                        self.tableView.deleteRows(at: [indexPath], with: .fade)
                        self.tableView.reloadData()
                    }
                }
                else{
                    NetworkController.AlertConnectionError(viewController: self)
                    self.removeRightButtons()
                    self.setEditing(false, animated: false)
                }
            }
        }
    }
    
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        self.tableView.setEditing(editing, animated: animated)
    }
}
