//
//  ESCalculatorViewController.swift
//  Pods
//
//  Created by Alex Koziaruk on 26.11.2020.
//  Copyright (c) 2020 Noisy Miner. All rights reserved.
//
//  This file was generated by the Clean Swift Xcode Templates so
//  you can apply clean architecture to your iOS and Mac projects,
//  see http://clean-swift.com
//

import UIKit

protocol ESCalculatorDisplayLogic: class {
    func display(newViewModel: ESCalculator.ViewModel)
}

class ESCalculatorViewController: UIViewController, ESCalculatorDisplayLogic {
    var interactor: ESCalculatorBusinessLogic?
    var router: (NSObjectProtocol & ESCalculatorRoutingLogic & ESCalculatorDataPassing)?

    var viewModel: ESCalculator.ViewModel?

//MARK: Outlets

    @IBOutlet weak var amountTextField: UITextField!
    @IBOutlet weak var symbolLabel: UILabel!
    @IBOutlet weak var currencyValueLabel: UILabel!
    @IBOutlet weak var yearlyIncomeLabel: UILabel!
    @IBOutlet weak var validatorValueLabel: UILabel!
    @IBOutlet weak var feeLabel: UILabel!
    @IBOutlet weak var dailyIncomeValueLabel: UILabel!
    @IBOutlet weak var mounthlyIncomeValueLabel: UILabel!
    @IBOutlet weak var yearlyIncomeValueLabel: UILabel!
    @IBOutlet weak var includeValidatorFeeSwitch: UISwitch!
    @IBOutlet weak var reinvestEarningsSwitch: UISwitch!
    @IBOutlet weak var validatorContainer: ESGradientView!
    
//MARK: Actions
    
    @IBAction func backButtonPressed() {
        navigationController?.popViewController(animated: true)
    }
    
    @IBAction func currencyButtonPressed() {
        router?.selectCurrency()
    }
    
    @IBAction func validatorButtonPressed() {
        router?.selectValidator()
    }
    
    @IBAction func proceedToStakingButtonPressed() {
        router?.proceedToStakingWith(amount: viewModel?.amountToStake ?? 0)
    }
    
    @IBAction func reinvestEarningsSwitchValueChanged() {
        update()
    }
    
    @IBAction func includeValidatorFeeSwitchValueChanged() {
        update()
    }
    
    //Required by keyboard done button
    @objc func doneButtonAction() {
        amountTextField.resignFirstResponder()
        update()
    }
    
// MARK: View lifecycle
  
    override func viewDidLoad() {
        super.viewDidLoad()
        amountTextField.addDoneButtonWith(width: view.frame.width, target: self,
                                          selector: #selector(ESCalculatorViewController.doneButtonAction))
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        interactor?.getData()
    }
    
    
//MARK: - ESCalculatorDisplayLogic
    
    func display(newViewModel: ESCalculator.ViewModel) {
        viewModel = newViewModel
        update()
    }
    

//MARK: - Update UI
    
    private func update() {
        
        self.viewModel?.includeValidatorFee = includeValidatorFeeSwitch.isOn
        self.viewModel?.includeReinvestment = reinvestEarningsSwitch.isOn
        self.viewModel?.amountToStake = Double(amountTextField.text ?? "0") ?? 0
        self.viewModel?.updateCalculations()
        
        guard let viewModel = viewModel else { return }

        symbolLabel.text = viewModel.symbol
        validatorValueLabel.text = viewModel.validatorName
        yearlyIncomeLabel.text = viewModel.yearlyIncom
        currencyValueLabel.text = viewModel.currency
        feeLabel.isHidden = !viewModel.showFee
        feeLabel.text = viewModel.displayValidatorFee
        dailyIncomeValueLabel.text = viewModel.displayPerDayAmount
        mounthlyIncomeValueLabel.text = viewModel.displayPerMonthAmount
        yearlyIncomeValueLabel.text = viewModel.displayPerYearAmount
        validatorContainer.isHidden = viewModel.validatorSelectorIsHidden
    }
    
}
