//
//  EntranceGoods.swift
//  iPOS
//
//  Created by Zoran Albu on 8/20/19.
//  Copyright © 2019 BBC. All rights reserved.
//

import Foundation
import ObjectMapper

enum RowState: Int{
    case Unchanged = 0
    case Modified = 1
    case Deleted = 2
    case Inserted = 3
    case Detached = 4
}

class EntranceGoods: Mappable{
    var entranceGoodsId: UUID =  UUID()
    var documentNumber: String = ""
    var documentDate: Date = Date()
    var entranceNumber: String?
    var certifyTime: Date?
    var sourceSupplierId: Int?
    var sourceSupplierName: String?
    var sourceStoreId: Int?
    var sourceStoreName: String?
    var documentQuantity: Int?
    var revealedQuantity: Int?
    var entranceGoodsOrderDetails: [EntranceGoodsDetails] = []
    
    var rowState: RowState = .Unchanged
    
    var isCertified: Bool{
        get{
            if let _ = self.certifyTime, let _ = self.entranceNumber{
                return true
            }
            return false
        }
    }

    var differenceQuantity: Int{
        get{
            let docQuantity = self.documentQuantity ?? 0
            let revQuantity = self.revealedQuantity ?? 0
            let diffQuantity = revQuantity - docQuantity
            return diffQuantity
        }
    }
    
    var detailDocumentQuantity: Int{
        get{
            var quantity: Int = 0
            for detail in self.entranceGoodsOrderDetails{
                quantity += detail.productDocumentQuantity
            }
            return quantity
        }
    }
    
    var detailRevealedQuantity: Int{
        get{
            var quantity: Int = 0
            for detail in self.entranceGoodsOrderDetails{
                quantity += detail.productRevealedQuantity
            }
            return quantity
        }
    }
    
    var detailDifferenceQuantity: Int{
        get{
            let diffQuantity = self.detailRevealedQuantity - self.detailDocumentQuantity
            return diffQuantity
        }
    }
    
    var detailDocumentQuantityPrice: Double{
        get{
            var total: Double = 0
            for detail in self.entranceGoodsOrderDetails{
                total += (detail.productSalePrice * Double(detail.productDocumentQuantity))
            }
            return total
        }
    }
    
    var detailRevealedQuantityPrice: Double{
        get{
            var total: Double = 0
            for detail in self.entranceGoodsOrderDetails{
                total += (detail.productSalePrice * Double(detail.productRevealedQuantity))
            }
            return total
        }
    }
    
    var detailDifferenceQuantityPrice: Double{
        get{
            var total: Double = 0
            total = self.detailRevealedQuantityPrice - self.detailDocumentQuantityPrice
            return total
        }
    }
    
    required init?(map: Map) {
        
    }
    
    init(){
        self.rowState = .Inserted
    }
    
    func mapping(map: Map) {
        entranceGoodsId <- (map["EntranceGoodsId"], Util.CustomUuidTransform())
        documentNumber <- map["DocumentNumber"]
        documentDate <- (map["DocumentDate"], Util.CustomDateTransform())
        entranceNumber <- map["EntranceNumber"]
        certifyTime <- (map["CertifyTime"], Util.CustomDateTransform())
        sourceSupplierId <- map["SourceSupplierId"]
        sourceSupplierName <- map["SourceSupplierName"]
        sourceStoreId <- map["SourceStoreId"]
        sourceStoreName <- map["SourceStoreName"]
        documentQuantity <- map["DocumentQuantity"]
        revealedQuantity <- map["RevealedQuantity"]
        entranceGoodsOrderDetails <- map["EntranceGoodsDetails"]
    }
}

class EntranceGoodsResponse: Mappable {
    var entranceGoods: [EntranceGoods]?
    var errorMessage: String?
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        entranceGoods <- map["EntranceGoods"]
        errorMessage <- map["ErrorMessage"]
    }
}

class EntranceGoodsDetails: Mappable{
    
    var entranceGoodsId: UUID =  UUID()
    var entranceGoodsDetailId: UUID =  UUID()
    var productId: String = ""
    var productCode: String = ""
    var productName: String = ""
    var productSalePrice: Double = 0
    var productSaleDiscount: Double = 0
    var productDocumentQuantity: Int = 0
    var productRevealedQuantity: Int = 0

    var rowState: RowState = .Unchanged
    
    required init?(map: Map) {
        
    }
    
    init(){
        self.rowState = .Inserted
    }
    
    func mapping(map: Map) {
        entranceGoodsId <- (map["EntranceGoodsId"], Util.CustomUuidTransform())
        entranceGoodsDetailId <- (map["EntranceGoodsDetailId"], Util.CustomUuidTransform())
        productId <- map["ProductId"]
        productCode <- map["ProductCode"]
        productName <- map["ProductName"]
        productSalePrice <- (map["ProductSalePrice"], Util.CustomDoubleTransform())
        productSaleDiscount <- (map["ProductSaleDiscount"], Util.CustomDoubleTransform())
        productDocumentQuantity <- map["ProductDocumentQuantity"]
        productRevealedQuantity <- map["ProductRevealedQuantity"]
    }
    
    var differenceQuantity: Int{
        get{
            return productRevealedQuantity - productDocumentQuantity
        }
    }
    
    var totalSalePrice: Double{
        get{
            return Double(productRevealedQuantity) * productSalePrice
        }
    }
    
    func setNewRowState(from action: String){
        if action == "modify"{
            if self.rowState == .Unchanged{
                self.rowState = .Modified
            }
        }
        else if action == "delete"{
            if self.rowState == .Unchanged || self.rowState == .Modified{
                self.rowState = .Deleted
            }
            else if self.rowState == .Inserted{
                self.rowState = .Detached
            }
        }
    }
}

class EntranceGoodsAndDetailsResponse: Mappable {
    var entranceGoods: EntranceGoods?
    var errorMessage: String?
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        entranceGoods <- map["EntranceGoodsAndDetails"]
        errorMessage <- map["ErrorMessage"]
    }
}
