import Foundation

public enum AddedBy: String, CustomStringConvertible, CaseIterable {
    case anyone
    case me
    case anyoneButMe
    
    public var description: String {
        switch self {
        case .anyone: return NSLocalizedString("Anyone", comment: "Added by")
        case .me: return NSLocalizedString("Me", comment: "Added by")
        case .anyoneButMe: return NSLocalizedString("Anyone but me", comment: "Added by")
        }
    }
}

public enum SortParameter: String, CustomStringConvertible, CaseIterable {
    case addedDate
    case updatedDate
    
    public var description: String {
        switch self {
        case .addedDate: return NSLocalizedString("added time", comment: "sort by added time")
        case .updatedDate: return NSLocalizedString("updated time", comment: "sort by updated time")
        }
    }
}

public enum SortOrder: String, CustomStringConvertible, CaseIterable {
    case ascending
    case descending
    
    public var description: String {
        switch self {
        case .ascending: return NSLocalizedString("ascending", comment: "sort order")
        case .descending: return NSLocalizedString("descending", comment: "sort order")
        }
    }
}

public struct Filter {
    public var searchString = ""
    public var brand: String?
    public var model: String?
    public var color: String?
    public var year: String?
    public var regions: [Int]?
    public var addedBy: AddedBy?
    public var sortBy: SortParameter? = .updatedDate
    public var sortOrder: SortOrder? = .descending
    public var fromDate: Date?
    public var toDate: Date?
    public var fromDateUpdated: Date?
    public var toDateUpdated: Date?
    
    public init() {
    }
    
    public 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
    }
    
    public 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
    }
}
