//
//  ContactDuplicatesResponse.swift
//  iPOS
//
//  Created by Rat Raul on 25/02/2020.
//  Copyright © 2020 BBC. All rights reserved.
//

import Foundation
import ObjectMapper

class ContactDuplicatesResponse: Mappable {
    var odataMetadata: String?
    var value: [ContactDuplicate]?
    
    required init?(map: Map){
        
    }
    
    func mapping(map: Map) {
        odataMetadata <- map["odata.metadata"]
        value <- map["value"]
    }
}

class ContactDuplicate: Mappable{
    var ContactId: String
    var NumericId: Int?
    var FirstName: String = ""
    var LastName: String = ""
    var VatCode: String?
    var FiscalCode: String?
    var StreetName: String?
    var StreetNumber: String?
    var ApartmentNumber: Int?
    var PostalCode: String?
    var City: String?
    var RegionCode: String?
    var CountryId: Int?
    var Tel: String?
    var Mobile: String?
    var Fax: String?
    var Email: String?
    var BirthDate: Date?
    var BirthCity: String?
    var Gender: String?
    var Notes: String?
    var IsLegalPerson: Bool?
    var AuthMarketingNotifications: Bool?
    var AuthMarketingAnalysis: Bool?
    var AuthThirdPartiesAnalysis: Bool?
    var AuthSensitiveData: Bool?
    var FidelityCard: String?
    var FidelityCardTypeId: Int?
    var EIDestinationCode: String?
    var DuplicateKey: String?
        
    func mapping(map: Map) {
        ContactId <- map["ContactId"]
        NumericId <- map["NumericId"]
        FirstName <- map["FirstName"]
        LastName <- map["LastName"]
        VatCode <- map["VatCode"]
        FiscalCode <- map["FiscalCode"]
        StreetName <- map["StreetName"]
        StreetNumber <- map["StreetNumber"]
        ApartmentNumber <- map["ApartmentNumber"]
        PostalCode <- map["PostalCode"]
        City <- map["City"]
        RegionCode <- map["RegionCode"]
        CountryId <- map["CountryId"]
        Tel <- map["Tel"]
        Mobile <- map["Mobile"]
        Fax <- map["Fax"]
        Email <- map["Email"]
        BirthDate <- (map["BirthDate"], Util.CustomDateTransform())
        BirthCity <- map["BirthCity"]
        Gender <- map["Gender"]
        Notes <- map["Notes"]
        IsLegalPerson <- map["IsLegalPerson"]
        AuthMarketingNotifications <- map["AuthMarketingNotifications"]
        AuthMarketingAnalysis <- map["AuthMarketingAnalysis"]
        AuthSensitiveData <- map["AuthSensitiveData"]
        AuthThirdPartiesAnalysis <- map["AuthThirdPartiesAnalysis"]
        FidelityCard <- map["CardBarcode"]
        FidelityCardTypeId <- map["FidelityCardTypeId"]
        EIDestinationCode <- map["EIDestinationCode"]
        DuplicateKey <- map["DuplicateKey"]
    }
    
    required init?(map: Map) {
        ContactId = UUID().description
    }
    
    init() {
        ContactId = UUID().description
    }
    
    
    
    func getContactDuplicateInfo() -> String{
        var string: String = ""
        
        if let duplicateKey = self.DuplicateKey{
            string += duplicateKey + "\n"
        }
        
        if let isLegalPerson = self.IsLegalPerson, isLegalPerson{
            string += self.FirstName
            
            if let vatCode = self.VatCode, vatCode != ""{
                string += "\n" + vatCode
            }
        }
        else{
            string += self.getFullName()
            
            if let fiscalCode = self.FiscalCode, fiscalCode != ""{
                string += "\n" + fiscalCode
            }
        }
        
        if let street = self.getStreetNameAndNumber(){
            string += "\n" + street
        }
        
        if let address = self.getPostalCodeCityAndRegion(){
            string += "\n" + address
            
            if let country = self.getCountry(){
                string += ", " + country
            }
            
        }
        
        return string
    }
    
    func getFullName() -> String{
        if self.FirstName != ""{
            if self.LastName != ""{
                return self.FirstName + " " + self.LastName
            }
            else{
                return self.FirstName
            }
        }
        else if self.LastName != ""{
            return self.LastName
        }
        else{
            return ""
        }
    }
    
    func getStreetNameAndNumber() -> String?{
        if self.StreetName != nil && self.StreetName != ""{
            if self.StreetNumber != nil && self.StreetNumber != ""{
                return "\(self.StreetName!), \(self.StreetNumber!)"
            }
            else{
                return self.StreetName
            }
        }
        else{
            return nil
        }
    }
    
    func getPostalCodeCityAndRegion() -> String?{
        if self.PostalCode != nil && self.PostalCode != ""{
            if self.City != nil && self.City != ""{
                if self.RegionCode != nil && self.RegionCode != ""{
                    return self.PostalCode! + ", " + self.City! + " (" + self.RegionCode! + ")"
                }
                else{
                    return self.PostalCode! + ", " + self.City!
                }
            }
            else if self.RegionCode != nil && self.RegionCode != ""{
                return self.PostalCode! + " (" + self.RegionCode! + ")"
            }
            else{
                return self.PostalCode!
            }
        }
        else if self.City != nil && self.City != ""{
            if self.RegionCode != nil && self.RegionCode != ""{
                return self.City! + " (" + self.RegionCode! + ")"
            }
            else{
                return self.City!
            }
        }
        else if self.RegionCode != nil && self.RegionCode != ""{
            return " (" + self.RegionCode! + ")"
        }
        else{
            return nil
        }
    }
    
    func getCountry() -> String?{
        if State.countries != nil{
            for item in State.countries!{
                if self.CountryId != nil{
                    if item.CountryId == self.CountryId{
                        return item.Name
                    }
                }
                else{
                    if item.Code == settings.userSettings.countryCode{
                        return item.Name
                    }
                }
            }
        }
        // Even if is null, the value will be validated before save and the client must select a country.
        return nil
    }
}
