//
//  Vouchers.swift
//  iPOS
//
//  Created by Zoran Albu on 6/28/18.
//  Copyright © 2018 BBC. All rights reserved.
//

import Foundation
import ObjectMapper

enum VoucherStatus: Int{
    case Inactive = 0
    case ValidityNotStarted = 1
    case Active = 2
    case Used = 3
    case Expired = 4
    case Disabled = 5
    case MaxUseCountExceeded = 6
}

enum VoucherCategory: Int{
    case VDV = 0
    case VDP = 1
    case VPV = 2
    case GFT = 3
    case TRG = 4
}

enum VoucherType: Int{
    case ECN = 0
    case FIN = 1
    case DSC = 2
}

class VoucherInfo: Mappable{
    var generator: VoucherGenerator = VoucherGenerator()
    var voucherCode: String = String()
    var amount: Double = 0
    var status: VoucherStatus?
    var activationTime: Date?
    var validityStartDate: Date?
    var validityEndDate: Date?
    var createTime: Date?
    var currencyId: String = String()
    var securityCode: String = String()
    var salePaymentCategory: String = String()
    var transactionNumber: String = String()
    var resultCode: String = String()
    var resultMessage: String = String()
    var voucherAction: VoucherStates?
    
    //not mapped
    var initialAmount: Double = 0
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        generator <- map["Generator"]
        voucherCode <- map["VoucherCode"]
        amount <- map["Amount"]
        status <- (map["Status"],EnumTransform<VoucherStatus>())
        activationTime <- (map["ActivationTime"], Util.CustomDateTransform())
        validityEndDate <- (map["ValidityEndDate"], Util.CustomDateTransform())
        validityStartDate <- (map["ValidityStartDate"], Util.CustomDateTransform())
        createTime <- (map["CreateTime"], Util.CustomDateTransform())
        currencyId <- map["CurrencyId"]
        securityCode <- map["SecurityCode"]
        salePaymentCategory <- map["SalePaymentCategory"]
        transactionNumber <- map["TransactionNumber"]
        resultCode <- map["ResultCode"]
        resultMessage <- map["ResultMessage"]
    }
    
    var decodedCurrencyId: String{
        get{
            return Util.decodeCurrencyId(currencyId: self.currencyId)
        }
    }
    
    var startDate: Date{
        get{
            return self.validityStartDate ?? Date()
        }
    }
    
    var endDate: Date{
        get{
            if let ed = self.validityEndDate, ed > Date.init(timeIntervalSince1970: 0){
                return ed
            }
            return Calendar.current.date(byAdding: .day, value: self.generator.validityDays, to: startDate) ?? startDate
        }
    }
}

class VoucherGenerator: Mappable{
    var voucherGeneratorId: Int = 0
    var voucherGeneratorCode: String = String()
    var voucherGeneratorName: String = String()
    var categoryCode: VoucherCategory?
    var voucherType: VoucherType?
    var repositoryType: String = String()
    var repositoryWSAddress: String = String()
    var styleId: Int?
    var message: String = String()
    var isScalable: Bool = false
    var cumulative: Bool = false
    var isRefundable: Bool = false
    var isRedeemedByPromotion: Bool = false
    var minValue: Double? = 0
    var maxValue: Double? = 0
    var paymentTypeId: Int?
    var paymentTypeCode: String?
    var discountCauseId: Int?
    var validityDays: Int = 365
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        voucherGeneratorId <- map["VoucherGeneratorId"]
        voucherGeneratorCode <- map["VoucherGeneratorCode"]
        voucherGeneratorName <- map["VoucherGeneratorName"]
        categoryCode <- (map["Category"],EnumTransform<VoucherCategory>())
        voucherType <- (map["VoucherType"],EnumTransform<VoucherType>())
        repositoryType <- map["RepositoryType"]
        repositoryWSAddress <- map["RepositoryWSAddress"]
        styleId <- map["StyleId"]
        message <- map["Message"]
        isScalable <- map["IsScalable"]
        cumulative <- map["Cumulative"]
        isRefundable <- map["IsRefundable"]
        isRedeemedByPromotion <- map["IsRedeemedByPromotion"]
        minValue <- (map["MinValue"], Util.CustomDoubleTransform())
        maxValue <- (map["MaxValue"], Util.CustomDoubleTransform())
        paymentTypeId <- map["PosPaymentTypeId"]
        paymentTypeCode <- map["PosPaymentTypeCode"]
        discountCauseId <- map["DiscountCauseId"]
        validityDays <- map["ValidityDays"]
    }
    
    var messageLines: [String]{
        get{
            if self.message != ""{
                return self.message.components(separatedBy: "\r\n")
            }
            return []
        }
    }
}

class ValidateVoucherRequest: Mappable {
    var storeId: Int = Int(settings.StoreId)!
    var cashRegister: Int = Int(settings.CashRegister)!
    var voucherCode: String = String()
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        storeId <- map["StoreId"]
        cashRegister <- map["CashRegister"]
        voucherCode <- map["VoucherCode"]
    }
}

class ValidateVoucherResponse: Mappable {
    var voucherInfo: VoucherInfo = VoucherInfo()
    var resultCode: String = String()
    var resultMessage: String = String()
    var resultErrorDetails: String = String()
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        voucherInfo <- map["VoucherInfo"]
        resultCode <- map["ResultCode"]
        resultMessage <- map["ResultMessage"]
        resultErrorDetails <- map["ResultErrorDetails"]
    }
}

class RedeemVoucherRequest: Mappable {
    var storeId: Int = Int(settings.StoreId)!
    var cashRegister: Int = Int(settings.CashRegister)!
    
    var voucherCode: String = String()
    var transactionNumber: Int = 0
    var receiptDay: Date = Date()
    var receiptId: UUID = UUID()
    var voucherValue: Double = 0
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        storeId <- map["StoreId"]
        cashRegister <- map["CashRegister"]
        
        voucherCode <- map["VoucherCode"]
        transactionNumber <- map["TransactionNumber"]
        receiptDay <- map["ReceiptDay"]
        receiptId <- map["ReceiptId"]
        voucherValue <- map["VoucherValue"]
    }
}

class RedeemVoucherResponse: Mappable {
    var resultCode: String = String()
    var resultMessage: String = String()
    var resultErrorDetails: String = String()
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        resultCode <- map["ResultCode"]
        resultMessage <- map["ResultMessage"]
        resultErrorDetails <- map["ResultErrorDetails"]
    }
}
class CreateVoucherRequest: Mappable {
    var voucherGeneratorCode: String = ""
    var amount: Double = 0
    var storeId: Int = Int(settings.StoreId) ?? -1
    var cashRegister: Int = Int(settings.CashRegister) ?? -1
    var transactionNumber: Int = 0
    var transactionTime: Date = Date()
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        voucherGeneratorCode <- map["VoucherGeneratorCode"]
        amount <- map["Amount"]
        storeId <- map["StoreId"]
        cashRegister <- map["CashRegister"]
        transactionNumber <- map["TransactionNumber"]
        transactionTime <- map["TransactionTime"]
    }
}

class CreateVoucherResponse: Mappable {
    var voucherInfo: VoucherInfo? = nil
    var resultCode: String = String()
    var resultMessage: String = String()
    var resultErrorDetails: String? = nil
    
    init() {
    }
    
    required init?(map: Map){
    }
    
    func mapping(map: Map) {
        voucherInfo <- map["VoucherInfo"]
        resultCode <- map["ResultCode"]
        resultMessage <- map["ResultMessage"]
        resultErrorDetails <- map["ResultErrorDetails"]
    }
}
