//
//  PromoShoppingCart.swift
//  iPOS
//
//  Created by bbc on 28/11/16.
//  Copyright © 2016 BBC. All rights reserved.
//

import Foundation
import ObjectMapper

class PromoShoppingCart : Mappable{
    // input fields
    var items: [PromoCartItem]
    var vouchers: [CartVouchers] = []
    var transactionNumber: Int?
    var storeId: Int?
    var cashRegister: Int?
    var payments: [Payment] = [Payment]()
    var contactId: UUID?
    var fidelityCardId: UUID?
    var clientCode: String?
    var clientFiscalCode: String?
    var paybackCard: PaybackCard?
    var transactionTime: String!
    var allowSendToHealthInsurance: Bool?
    var transactionSign: Int?
    var cashierUserId: UUID?
    var receiptFiscalNumber: Int?
    var zReportNumber: Int?
    var fiscalPrinterSN: String?
    var restValue: Double = 0
    var usedPoints: Int?
    var effects: [Effect]?
    var receiptLines: String?
    var receiptType: String = ""
    var hasLotteryCode: Bool = false
    var effectVouchers: [EffectVoucher] = []
    var issuedVoucherCode: String?
    
    init() {
        items = [PromoCartItem]()
    }
    
    required init(map:Map) {
        items = [PromoCartItem]()
    }
    
    func mapping(map: Map) {
        items <- map["Items"]
        vouchers <- map["CartVouchers"]
        clientCode <- map["ClientCode"]
        clientFiscalCode <- map["ClientFiscalCode"]
        contactId <- (map["ContactId"], Util.CustomUuidTransform())
        fidelityCardId <- (map["FidelityCardId"], Util.CustomUuidTransform())
        effects <- map["Effects"]
        transactionNumber <- map["TransactionNumber"]
        storeId <- map["StoreId"]
        cashRegister <- map["CashRegister"]
        payments <- map["Payments"]
        paybackCard <- map["PaybackCard"]
        transactionTime <- map["TransactionTime"]
        allowSendToHealthInsurance <- map["AllowSendToHealthInsurance"]
        transactionSign <- map["TransactionSign"]
        cashierUserId <- (map["CashierUserId"], Util.CustomUuidTransform())
        receiptFiscalNumber <- map["ReceiptFiscalNumber"]
        restValue <- (map["RestValue"], Util.CustomDoubleTransform())
        usedPoints <- map["UsedPoints"]
        receiptLines <- map["ReceiptLines"]
        receiptType <- map["ReceiptType"]
        zReportNumber <- map["ZReportNumber"]
        fiscalPrinterSN <- map["FiscalPrinterSN"]
        hasLotteryCode <- map["HasLotteryCode"]
        effectVouchers <- map["EffectVouchers"]
        issuedVoucherCode <- map["IssuedVoucherCode"]
    }
}

class PromoCartItem : Mappable {
    //input fields
    var rowNumber: Int!
    var productCode: String!
    var productName: String!
    var barcode: String!
    var styleId: Int!
    var originalUnitValueVatIncluded: Decimal!
    var quantity: Double
    var canApplyPromotions: Bool!
    var vatId: Int!
    var newVatRate: Double!
    var originalVatRate: Double!
    var fiscalExpenseTypeId: Int?
    var serialNumber: String?
    var rowType: String = ""
    var itemTemplateCode = ""
    var productFamilyName: String!
    var productFamilyCode: String!
    var productSubfamilyName: String!
    var productSubfamilyCode: String!
    
    // output fields
    var grossUnitValueVatIncluded: Double?
    var rowDiscountValue: Double?
    var distributedDiscount: Double = 0
    var netRowValueVatIncluded: Double?
    var effects: [Effect]?
    
    init() {
        rowNumber = 1
        quantity = 1
        canApplyPromotions = true
    }
    required init ?(map: Map) {
        rowNumber = 1
        quantity = 1
        canApplyPromotions = true
    }
    
    func mapping(map: Map) {
        rowNumber <- map["RowNumber"]
        productCode <- map["ProductCode"]
        productName <- map["ProductName"]
        styleId <- map["StyleId"]
        barcode <- map["Barcode"]
        vatId <- map["VatId"]
        newVatRate <- map["NewVatRate"]
        originalVatRate <- map["OriginalVatRate"]
        originalUnitValueVatIncluded <- (map["OriginalUnitValueVatIncluded"])
        quantity <- (map["Quantity"], Util.CustomDoubleTransform())
        canApplyPromotions <- map["CanApplyPromotions"]
        fiscalExpenseTypeId <- map["FiscalExpenseTypeId"]
        serialNumber <- map["SerialNumber"]
        
        grossUnitValueVatIncluded <- (map["GrossUnitValueVatIncluded"])
        rowDiscountValue <- (map["RowDiscountValue"])
        distributedDiscount <- map["DistributedDiscount"]
        netRowValueVatIncluded <- (map["NetRowValueVatIncluded"])
        effects <- map["Effects"]
        
        rowType <- map["RowType"]
        itemTemplateCode <- map["ItemTemplateCode"]
        
        productFamilyName <- map["ProductFamilyName"]
        productFamilyCode <- map["ProductFamilyCode"]
        productSubfamilyName <- map["ProductSubfamilyName"]
        productSubfamilyCode <- map["ProductSubfamilyCode"]
    }
}

class Effect: Mappable {
    var rowNumber: Int
    var promotionId: Int?
    var promotionName: String?
    var rowDiscountValue: Double
    var rowDiscountPercent: Double
    var effectCategory: String?
    var promotionCode: String?
    var triggerVoucherCode: String?
    var discountCauseId: Int
    var isManualDiscount = false
    var effectVoucherGeneratorId: Int
    //var effectVoucherGeneratorCode: String?
    init() {
        rowNumber = 1
        rowDiscountValue = 0
        rowDiscountPercent = 0
        discountCauseId = 0
        effectVoucherGeneratorId = 0
    }
    
    required init(map: Map) {
        rowNumber = 1
        rowDiscountValue = 0
        rowDiscountPercent = 0
        discountCauseId = 0
        effectVoucherGeneratorId = 0
    }
    
    func mapping(map: Map) {
        rowNumber <- map["RowNumber"]
        promotionId <- map["PromotionId"]
        promotionName <- map["PromotionName"]
        rowDiscountValue <- map["RowDiscountValue"]
        rowDiscountPercent <- map["RowDiscountPercent"]
        effectCategory <- map["EffectCategory"]
        promotionCode <- map["PromotionCode"]
        triggerVoucherCode <- map["TriggerVoucherCode"]
        discountCauseId <- map["DiscountCauseId"]
        effectVoucherGeneratorId <- map["EffectVoucherGeneratorId"]
        //effectVoucherGeneratorCode <- map["EffectVoucherGeneratorCode"]
    }
}

class Payment: Mappable{
    var paymentTypeId: Int!
    var name: String!
    var amount: Double!
    var STAN: String?
    var PAN: String?
    var Ticket: String?
    var VoucherCode: String?
    var VoucherGeneratorId: Int?
    var VoucherCategoryCode: String?
    var VoucherType: String?
    var loanNo : String?
    
    var paymentType: PaymentType = PaymentType()
    
    //not mapped
    var voucherInfo: VoucherInfo? = nil
    
    init() {
    }
    
    required init(map: Map) {
    }
    
    func mapping(map: Map) {
        paymentTypeId <- map["PaymentTypeId"]
        amount <- map["Amount"]
        STAN <- map["STAN"]
        PAN <- map["PAN"]
        Ticket <- map["Ticket"]
        VoucherCode <- map["VoucherCode"]
        VoucherGeneratorId <- map["VoucherGeneratorId"]
        VoucherCategoryCode <- map["VoucherCategoryCode"]
        VoucherType <- map["VoucherType"]
        loanNo <- map["LoanNo"]
    }
}

class CartVouchers: Mappable{
    var voucherCode: String = ""
    var categoryCode: VoucherCategory?
    var voucherStatus: VoucherStatus?
    var amount: Double?
    var voucherAction: String = "R" // R-Redeem; A-Activate
    
    init() {
    }
    
    init(voucherCode: String, categoryCode: VoucherCategory, voucherStatus: VoucherStatus, amount: Double, action: String) {
        self.voucherCode = voucherCode
        self.categoryCode = categoryCode
        self.voucherStatus = voucherStatus
        self.amount = amount
        self.voucherAction = action
    }
    
    required init(map: Map) {
    }
    
    func mapping(map: Map) {
        voucherCode <- map["VoucherCode"]
        categoryCode <- (map["CategoryCode"],EnumTransform<VoucherCategory>())
        voucherStatus <- (map["VoucherStatus"],EnumTransform<VoucherStatus>())
        amount <- (map["Amount"], Util.CustomDoubleTransform())
        voucherAction <- map["VoucherAction"]
    }
}
