//
//  List.swift
//  Pachyderm
//
//  Created by Shadowfacts on 9/9/18.
//  Copyright © 2018 Shadowfacts. All rights reserved.
//

import Foundation

public class List: Decodable, Equatable, Hashable {
    public let id: String
    public let title: String
    
    public var timeline: Timeline {
        return .list(id: id)
    }
    
    public static func ==(lhs: List, rhs: List) -> Bool {
        return lhs.id == rhs.id
    }
    
    public func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
    public static func getAccounts(_ list: List, range: RequestRange = .default) -> Request<[Account]> {
        var request = Request<[Account]>(method: .get, path: "/api/v1/lists/\(list.id)/accounts")
        request.range = range
        return request
    }
    
    public static func update(_ list: List, title: String) -> Request<List> {
        return Request<List>(method: .put, path: "/api/v1/lists/\(list.id)", body: ParametersBody(["title" => title]))
    }
    
    public static func delete(_ list: List) -> Request<Empty> {
        return Request<Empty>(method: .delete, path: "/api/v1/lists/\(list.id)")
    }
    
    public static func add(_ list: List, accounts accountIDs: [String]) -> Request<Empty> {
        return Request<Empty>(method: .post, path: "/api/v1/lists/\(list.id)/accounts", body: ParametersBody(
                "account_ids" => accountIDs
            ))
    }
    
    public static func remove(_ list: List, accounts accountIDs: [String]) -> Request<Empty> {
        return Request<Empty>(method: .delete, path: "/api/v1/lists/\(list.id)/accounts", body: ParametersBody(
                "account_ids" => accountIDs
            ))
    }
    
    private enum CodingKeys: String, CodingKey {
        case id
        case title
    }
}