//
//  ProfileInteractor.swift
//  ExchangeApp
//
//  Created by Юрий Нориков on 04.12.2019.
//  Copyright (c) 2019 norikoff. 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 ProfileBusinessLogic
{
    func checkKeys(request: Profile.Something.Request)
    func clearKeys()
    func checkApi()
}

protocol ProfileDataStore
{
    //var name: String { get set }
}

class ProfileInteractor: ProfileBusinessLogic, ProfileDataStore
{
    var presenter: ProfilePresentationLogic?
    //var name: String = ""
    
    // MARK: Do something
    
    func checkApi(){
        if UserDefaults.standard.value(forKey: "key") != nil && UserDefaults.standard.value(forKey: "secret") != nil {
            let apikey = UserDefaults.standard.value(forKey: "key") as! String
            let secret = UserDefaults.standard.value(forKey: "secret") as! String
            if !apikey.isEmpty || !secret.isEmpty{
                let response = Profile.Something.Response(errorMessage: nil, message: "Avalible keys")
                self.presenter?.presentMessage(response: response)
            }
        } else {
            
        }
    }
    
    func clearKeys(){
        UserDefaults.standard.set("", forKey: "key")
        UserDefaults.standard.set("", forKey: "secret")
        self.presenter?.presentClear()
    }
    
    
    func checkKeys(request: Profile.Something.Request){
        let utils = UtilsService()
        let worker = PoloniexApiService(utilService: utils)
        UserDefaults.standard.set(request.apiKey, forKey: "key")
        UserDefaults.standard.set(request.secretKey, forKey: "secret")
        worker.getWallet(){ result in
            switch result {
            case .success:
                let response = Profile.Something.Response(errorMessage: nil, message: "Avalible keys")
                self.presenter?.presentMessage(response: response)
            case .failure:
                UserDefaults.standard.set("", forKey: "key")
                UserDefaults.standard.set("", forKey: "secret")
                let response = Profile.Something.Response(errorMessage: "Bad keys", message: nil)
                self.presenter?.presentError(response: response)
            }
        }
    }
}
