import Foundation

struct Filter {
    var searchString = ""
    var brand: String?
    var model: String?
    var color: String?
    var year: String?
    var regions: [Int]?
    var addedBy: AddedBy?
    var sortBy: SortParameter? = .updatedDate
    var sortOrder: SortOrder? = .descending
    var fromDate: Date?
    var toDate: Date?
    var fromDateUpdated: Date?
    var toDateUpdated: Date?
    
    mutating func clear() {
        self.brand = nil
        self.model = nil
        self.color = nil
        self.year = nil
        self.regions = nil
        self.addedBy = nil
        self.sortBy = .updatedDate
        self.sortOrder = .descending
        self.fromDate = nil
        self.toDate = nil
        self.fromDateUpdated = nil
        self.toDateUpdated = nil
    }
    
    func queryDictionary() -> [String: String] {
        var dict: [String: String] = ["query": self.searchString]
        
        if let brand = self.brand {
            dict["brand"] = brand
        }
        if let model = self.model {
            dict["model"] = model
        }
        if let color = self.color {
            dict["color"] = color
        }
        if let year = self.year {
            dict["year"] = year
        }
        if let regions = self.regions {
            dict["regions"] = regions.map(String.init).joined(separator: ",")
        }
        if let addedBy = self.addedBy {
            dict["addedBy"] = addedBy.rawValue
        }
        if let sortBy = self.sortBy {
            dict["sortBy"] = sortBy.rawValue
        }
        if let sortOrder = self.sortOrder {
            dict["sortOrder"] = sortOrder.rawValue
        }
        if let fromDate = self.fromDate {
            dict["fromDate"] = String(fromDate.timeIntervalSince1970)
        }
        if let toDate = self.toDate {
            dict["toDate"] = String(toDate.timeIntervalSince1970)
        }
        if let fromDateUpdated = self.fromDateUpdated {
            dict["fromDateUpdated"] = String(fromDateUpdated.timeIntervalSince1970)
        }
        if let toDateUpdated = self.toDateUpdated {
            dict["toDateUpdated"] = String(toDateUpdated.timeIntervalSince1970)
        }
        
        return dict
    }
}
