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

import Foundation
import ObjectMapper
import UIKit

class WSBarcodeInfo: Mappable {
    var ProductId: String
    var Code: String
    var Name: String
    var Barcode: String?
    var StyleId: Int?
    var StoreId: Int
    var ColorName: String?
    var SizeCode: String?
    var SalePrice: Double = 0
    var Discount: Double?
    var FiscalPrinterDepartment: Int?
    var VatId: Int?
    var VatRate: Double?
    var WOVatId: Int?
    var WOVatRate: Double?
    var UnitOfMeasure: String?
    var DecimalPlaces: Int?
    var IsPriceChangeRequired: Bool?
    var IsPosSaleEnabled: Bool?
    var IsOutOfStockSaleEnabled: Bool?
    var IsWorkOrderSaleEnabled: Bool?
    var IsDiscountEnabled: Bool?
    var MedicalDevice: Bool?
    var AvailableStockQuantity: Int?
    var Description: String?
    var ImageIcon: UIImage?
    var FiscalExpenseTypeId: Int?
    var FiscalExpenseTypeCode: String?
    var hasSerialNumber: Bool = false
    var serialNumber: String?
    var productFamilyId: Int = 0
    var productFamilyCode: String = String()
    var productFamilyName: String = String()
    var productSubfamilyId: Int = 0
    var productSubfamilyCode: String = String()
    var productSubfamilyName: String = String()
    var Product: String = ""
    var productContainerQuantity: Int = 1
    
    // not mapped.
    var quantity: Double = 1
    
    func mapping(map: Map) {
        ProductId <- map["ProductId"]
        Code <- map["Code"]
        Name <- map["Name"]
        Barcode <- map["Barcode"]
        StyleId <- map["StyleId"]
        StoreId <- map["StoreId"]
        ColorName <- map["ColorName"]
        SizeCode <- map["SizeCode"]
        SalePrice <- (map["SalePrice"], Util.CustomDoubleTransform())
        Discount <- (map["Discount"], Util.CustomDoubleTransform())
        FiscalPrinterDepartment <- map["FiscalPrinterDepartment"]
        VatId <- map["VatId"]
        VatRate <- (map["VatRate"], Util.CustomDoubleTransform())
        WOVatId <- map["WOVatId"]
        WOVatRate <- (map["WOVatRate"], Util.CustomDoubleTransform())
        UnitOfMeasure <- map["UnitOfMeasure"]
        DecimalPlaces <- map["DecimalPlaces"]
        IsPriceChangeRequired <- map["IsPriceChangeRequired"]
        IsPosSaleEnabled <- map["IsPosSaleEnabled"]
        IsOutOfStockSaleEnabled <- map["IsOutOfStockSaleEnabled"]
        IsWorkOrderSaleEnabled <- map["IsWorkOrderSaleEnabled"]
        IsDiscountEnabled <- map["IsDiscountEnabled"]
        MedicalDevice <- map["MedicalDevice"]
        AvailableStockQuantity <- map["AvailableStockQuantity"]
        ImageIcon <- (map["ImageIcon"], Util.CustomImageTransform())
        Description <- map["Description"]
        FiscalExpenseTypeId <- map["FiscalExpenseTypeId"]
        FiscalExpenseTypeCode <- map["FiscalExpenseTypeCode"]
        hasSerialNumber <- map["HasSerialNumber"]
        serialNumber <- map["SerialNumber"]
        productFamilyId <- map["ProductFamilyId"]
        productFamilyCode <- map["ProductFamilyCode"]
        productFamilyName <- map["ProductFamilyName"]
        productSubfamilyId <- map["ProductSubfamilyId"]
        productSubfamilyCode <- map["ProductSubfamilyCode"]
        productSubfamilyName <- map["ProductSubfamilyName"]
        productContainerQuantity <- map["Quantity"]
        Product <- map["Product"]
    }
    
    required init?(map: Map) {
        ProductId = ""
        Code = ""
        StoreId = 1
        Name = ""
    }
    
    init() {
        ProductId = ""
        Code = ""
        StoreId = 1
        Name = ""
    }
    
    func clone() -> WSBarcodeInfo {
        let clone = WSBarcodeInfo()
        clone.ProductId = self.ProductId
        clone.Code = self.Code
        clone.Name = self.Name
        clone.Barcode = self.Barcode
        clone.StyleId = self.StyleId
        clone.StoreId = self.StoreId
        clone.ColorName = self.ColorName
        clone.SizeCode = self.SizeCode
        clone.SalePrice = self.SalePrice
        clone.Discount = self.Discount
        clone.FiscalPrinterDepartment = self.FiscalPrinterDepartment
        clone.VatId = self.VatId
        clone.VatRate = self.VatRate
        clone.UnitOfMeasure = self.UnitOfMeasure
        clone.DecimalPlaces = self.DecimalPlaces
        clone.IsPriceChangeRequired = self.IsPriceChangeRequired
        clone.IsPosSaleEnabled = self.IsPosSaleEnabled
        clone.IsOutOfStockSaleEnabled = self.IsOutOfStockSaleEnabled
        clone.IsWorkOrderSaleEnabled = self.IsWorkOrderSaleEnabled
        clone.IsDiscountEnabled = self.IsDiscountEnabled
        clone.MedicalDevice = self.MedicalDevice
        clone.AvailableStockQuantity = self.AvailableStockQuantity
        clone.ImageIcon = self.ImageIcon
        clone.Description = self.Description
        clone.FiscalExpenseTypeId = self.FiscalExpenseTypeId
        clone.FiscalExpenseTypeCode = self.FiscalExpenseTypeCode
        clone.hasSerialNumber = self.hasSerialNumber
        clone.serialNumber = self.serialNumber
        clone.Product = self.Product
        clone.productFamilyCode = self.productFamilyCode
        clone.productFamilyName = self.productFamilyName
        clone.productSubfamilyCode = self.productSubfamilyCode
        clone.productSubfamilyName = self.productSubfamilyName
        
        clone.quantity = self.quantity
        clone.productContainerQuantity = self.productContainerQuantity
        
        return clone
    }
    
    func convertToOrderDetail(itemTemplate: ItemTemplate, order: WorkOrder, parent: WorkOrderDetail?, rowType: String) -> WorkOrderDetail{
        let item = WorkOrderDetail()
        
        item.OrderId = order.OrderId
        item.OrderedQuantity = self.quantity
        item.SaleState = 1
        item.OperationType = "V"
        item.PrintType = "F"
        item.UnitOfMeasureName = self.UnitOfMeasure
        item.StyleId = self.StyleId!
        item.OriginalPrice = self.SalePrice
        item.GrossUnitValueVatIncluded = self.SalePrice
        item.NetRowValueVatIncluded = self.SalePrice
        item.ProductName = self.Name
        item.ProductCode = self.Code
        item.Barcode = self.Barcode
        item.RowType = rowType
        item.OrderDetailRowNumber = order.getNextDetailRowNumber()
        item.productFamilyCode = self.productFamilyCode
        item.productFamilyName = self.productFamilyName
        item.productSubfamilyCode = self.productSubfamilyCode
        item.productSubfamilyName = self.productSubfamilyName
        
        if rowType == rowTypeEnum.glasses.rawValue{
            item.ProductName = itemTemplate.name
            item.FiscalExpenseTypeId = self.FiscalExpenseTypeId
            item.MedicalDevice = self.MedicalDevice
            item.VatId = self.VatId
            item.VatRate = self.VatRate
            item.VatId = self.WOVatId
            item.VatRate = self.WOVatRate
            item.PrintType = "N"
        }
        else{
            if let parent = parent{
                item.FiscalExpenseTypeId = parent.FiscalExpenseTypeId
                item.MedicalDevice = parent.MedicalDevice
                item.VatId = parent.VatId
                item.VatRate = parent.VatRate
            }
        }
        
        return item
    }
    
    var woVatRate: Double{
        get{
            var vatRate: Double = 0
            if let vr = self.WOVatRate{
                vatRate = vr
            }
            return (vatRate).rounded(toPlaces: 2)
        }
    }
    
    var netRowValueWOVatExcluded: Double{
        get{
            return (self.SalePrice / (1 + woVatRate)).rounded(toPlaces: 2)
        }
    }
    
    
    var woVatValue: Double{
        get{
            return  (netRowValueWOVatExcluded * woVatRate).rounded(toPlaces: 2)
        }
    }
}
