//
//  ExplorerApi.swift
//  SodaLive
//
//  Created by klaus on 2023/08/10.
//

import Foundation
import Moya

enum ExplorerApi {
    case getExplorer
    case searchChannel(channel: String)
    case getCreatorProfile(userId: Int)
    case getFollowerList(userId: Int, page: Int, size: Int)
    case getCreatorProfileCheers(userId: Int, page: Int, size: Int)
    case writeCheers(parentCheersId: Int?, creatorId: Int, content: String)
    case modifyCheers(request: PutModifyCheersRequest)
    case writeCreatorNotice(request: PostCreatorNoticeRequest)
    case getCreatorProfileDonationRanking(userId: Int, page: Int, size: Int)
}

extension ExplorerApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .getExplorer:
            return "/explorer"
            
        case .searchChannel:
            return "/explorer/search/channel"
            
        case .getCreatorProfile(let userId):
            return "/explorer/profile/\(userId)"
            
        case .getCreatorProfileDonationRanking(let userId, _, _):
            return "/explorer/profile/\(userId)/donation-rank"
            
        case .getFollowerList(let userId, _, _):
            return "/explorer/profile/\(userId)/follower-list"
            
        case .getCreatorProfileCheers(let userId, _, _):
            return "/explorer/profile/\(userId)/cheers"
            
        case .writeCheers:
            return "/explorer/profile/cheers"
            
        case .modifyCheers:
            return "/explorer/profile/cheers"
            
        case .writeCreatorNotice:
            return "/explorer/profile/notice"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getExplorer, .searchChannel, .getCreatorProfile, .getFollowerList, .getCreatorProfileCheers, .getCreatorProfileDonationRanking:
            return .get
            
        case .writeCheers, .writeCreatorNotice:
            return .post
            
        case .modifyCheers:
            return .put
        }
    }
    
    var task: Task {
        switch self {
        case .getExplorer:
            return .requestPlain
            
        case .searchChannel(let channel):
            return .requestParameters(parameters: ["channel" : channel], encoding: URLEncoding.queryString)
            
        case .getCreatorProfile:
            let parameters = ["timezone": TimeZone.current.identifier] as [String: Any]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .getFollowerList(_, let page, let size):
            let parameters = [
                "page": page - 1,
                "size": size
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .getCreatorProfileCheers(_, let page, let size):
            let parameters = [
                "page": page - 1,
                "size": size,
                "timezone": TimeZone.current.identifier,
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .writeCheers(let parentCheersId, let creatorId, let content):
            let request = PostWriteCheersRequest(parentId: parentCheersId, creatorId: creatorId, content: content)
            return .requestJSONEncodable(request)
            
        case .modifyCheers(let request):
            return .requestJSONEncodable(request)
            
        case .writeCreatorNotice(let request):
            return .requestJSONEncodable(request)
            
        case .getCreatorProfileDonationRanking(_, let page, let size):
            let parameters = [
                "page": page - 1,
                "size": size
            ] as [String : Any]
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
