//
//  OrderDetailInteractor.swift
//  smartPOS
//
//  Created by I Am Focused on 18/04/2021.
//  Copyright (c) 2021 Clean Swift LLC. 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 OrderDetailBusinessLogic {
    func getOrder(request: OrderDetail.GetOrder.Request)
    func confirmOrder(request: OrderDetail.ConfirmOrder.Request)
    func rejectOrder(request: OrderDetail.RejectOrder.Request)
    func completeOrder(request: OrderDetail.CompleteOrder.Request)
}

protocol OrderDetailDataStore {
    var order: Order? { get set }
    var orderItem: OrderItem? { get set }
}

class OrderDetailInteractor: OrderDetailBusinessLogic, OrderDetailDataStore {
    var presenter: OrderDetailPresentationLogic?
    var worker = OrderDetailWorker()
    var order: Order?
    var orderItem: OrderItem?
    let debugMode = false

    // MARK: Do something

    func getOrder(request: OrderDetail.GetOrder.Request) {
        guard let orderId = request.id else { return }
        var response: OrderDetail.GetOrder.Response!

        worker.ordersDataManager.getOrder(id: orderId, debugMode).done { orderRes in
            print(orderRes.data)
            if orderRes.statusCode >= 200 || orderRes.statusCode <= 300 {
                let data = orderRes.data
                response = OrderDetail.GetOrder.Response(order: data.order, error: nil)
            }
        }.catch { error in
            response = OrderDetail.GetOrder.Response(order: nil, error: OrderErrors.couldNotLoadOrderDetail(error: error.localizedDescription))
        }.finally {
            self.presenter?.presentOrder(response: response)
        }
    }

    func confirmOrder(request: OrderDetail.ConfirmOrder.Request) {
        var response: OrderDetail.ConfirmOrder.Response!
        guard let orderId = request.id else {
            response = OrderDetail.ConfirmOrder.Response(error: OrderErrors.couldNotConfirmOrder(error: "OrderId is invalid."))
            presenter?.presentConfirmedOrder(response: response)
            return
        }
        worker.ordersDataManager.confirmOrder(orderId: orderId, debugMode).done { orderRes in
            if orderRes.statusCode >= 200 || orderRes.statusCode <= 300 {
                response = OrderDetail.ConfirmOrder.Response(error: nil)
            }
        }.catch { error in
            response = OrderDetail.ConfirmOrder.Response(error: OrderErrors.couldNotConfirmOrder(error: error.localizedDescription))
        }.finally {
            self.presenter?.presentConfirmedOrder(response: response)
        }
    }

    func rejectOrder(request: OrderDetail.RejectOrder.Request) {
        var response: OrderDetail.RejectOrder.Response!
        guard let orderId = request.id else {
            response = OrderDetail.RejectOrder.Response(error: OrderErrors.couldNotConfirmOrder(error: "OrderId is invalid."))
            presenter?.presentRejectedOrder(response: response)
            return
        }

        worker.ordersDataManager.voidOrder(orderId: orderId, orderItemIds: request.orderItemIds, cashierNote: request.cashierNote, debugMode).done { orderRes in
            if orderRes.statusCode >= 200 || orderRes.statusCode <= 300 {
                response = OrderDetail.RejectOrder.Response(error: nil)
            }
        }.catch { error in
            response = OrderDetail.RejectOrder.Response(error: OrderErrors.couldNotConfirmOrder(error: error.localizedDescription))
        }.finally {
            self.presenter?.presentRejectedOrder(response: response)
        }
    }

    func completeOrder(request: OrderDetail.CompleteOrder.Request) {
        var response: OrderDetail.CompleteOrder.Response!
        guard let orderId = request.id else {
            response = OrderDetail.CompleteOrder.Response(error: OrderErrors.couldNotConfirmOrder(error: "OrderId is invalid."))
            presenter?.presentCompletedOrder(response: response)
            return
        }

        worker.ordersDataManager.completeOrder(orderId: orderId, debugMode).done { orderRes in
            if orderRes.statusCode >= 200 || orderRes.statusCode <= 300 {
                response = OrderDetail.CompleteOrder.Response(error: nil)
            }
        }.catch { error in
            response = OrderDetail.CompleteOrder.Response(error: OrderErrors.couldNotConfirmOrder(error: error.localizedDescription))
        }.finally {
            self.presenter?.presentCompletedOrder(response: response)
        }
    }
}
