//
//  CartVoucherViewController.swift
//  iPOS
//
//  Created by Zoran Albu on 4/18/19.
//  Copyright © 2019 BBC. All rights reserved.
//

import Foundation
import UIKit

class CartVoucherViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initData()
        self.addDelegates()
    }
    
    func initData(){
        self.title = Util.getString("Vouchers")
    }
    
    func addDelegates(){
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tableView.reloadData()
        
        self.tableView.setEditing(true, animated: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.tableView.reloadData()
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return State.shoppingCart.vouchersInfo.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
        let voucher = State.shoppingCart.vouchersInfo[indexPath.row]
        
        if indexPath.row % 2 == 0{
            cell.layer.backgroundColor = UIColor.cyan.cgColor
        }
        else{
            cell.layer.backgroundColor = UIColor.yellow.cgColor
        }
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15.0)
        cell.textLabel?.textAlignment = .center
        if voucher.generator.categoryCode == VoucherCategory.VDP{
            cell.textLabel?.text = voucher.voucherCode + " : " + "\(Util.DoubleToStringWithTwoDecimals(voucher.amount)) %"
        }
        else{
            cell.textLabel?.text = voucher.voucherCode + " : " + "\(Util.DoubleToStringWithTwoDecimals(voucher.amount)) \(settings.userSettings.defaultCurrency)"
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let removeCartVoucherAction = UITableViewRowAction(style: UITableViewRowAction.Style.default, title: Util.getString("Remove"), handler:{action, indexpath in
            let voucher = State.shoppingCart.vouchersInfo[indexPath.row]
            self.removeCartVoucher(voucher: voucher)
            self.resetCanApplyPromotions()
        })
        removeCartVoucherAction.backgroundColor = UIColor.red
        return [removeCartVoucherAction]
    }
    
    func removeCartVoucher(voucher: VoucherInfo) {
        for index in 0..<State.shoppingCart.vouchersInfo.count{
            if voucher.voucherCode == State.shoppingCart.vouchersInfo[index].voucherCode{
                State.shoppingCart.vouchersInfo.remove(at: index)
                self.tableView.reloadData()
                break
            }
        }
    }
    
    func resetCanApplyPromotions(){
        if State.shoppingCart.vouchersInfo.count == 0 {
            for item in State.shoppingCart.cartItems{
                item.canApplyPromotions = true
            }
        }
    }
}
