//
//  SeriesApi.swift
//  SodaLive
//
//  Created by klaus on 4/29/24.
//

import Foundation
import Moya

enum SeriesApi {
    case getSeriesList(creatorId: Int, sortType: SeriesListAllViewModel.SeriesSortType, page: Int, size: Int)
    case getSeriesDetail(seriesId: Int)
    case getSeriesContentList(seriesId: Int, page: Int, size: Int, sortType: SeriesListAllViewModel.SeriesSortType)
    case getRecommendSeriesList(isAdultContentVisible: Bool, contentType: ContentType)
}

extension SeriesApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .getSeriesList:
            return "/audio-content/series"
            
        case .getSeriesDetail(let seriesId):
            return "/audio-content/series/\(seriesId)"
            
        case .getSeriesContentList(let seriesId, _, _, _):
            return "/audio-content/series/\(seriesId)/content"
            
        case .getRecommendSeriesList:
            return "/audio-content/series/recommend"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getSeriesList, .getSeriesDetail, .getSeriesContentList, .getRecommendSeriesList:
            return .get
        }
    }
    
    var task: Moya.Task {
        switch self {
        case .getSeriesList(let creatorId, let sortType, let page, let size):
            let parameters = [
                "creatorId": creatorId,
                "sortType": sortType,
                "page": page - 1,
                "size": size
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .getSeriesDetail:
            return .requestPlain
            
        case .getRecommendSeriesList(let isAdultContentVisible, let contentType):
            let parameters = [
                "isAdultContentVisible": isAdultContentVisible,
                "contentType": contentType
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .getSeriesContentList(_, let page, let size, let sortType):
            let parameters = [
                "page": page - 1,
                "size": size,
                "sortType": sortType
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
