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

import Foundation
import Moya

enum MessageApi {
    case getSentTextMessage(page: Int, size: Int)
    case getReceivedTextMessage(page: Int, size: Int)
    case getKeepTextMessage(page: Int, size: Int)
    case sendTextMessage(request: SendTextMessageRequest)
    case keepTextMessage(messageId: Int)
    
    case getSentVoiceMessage(page: Int, size: Int)
    case getReceivedVoiceMessage(page: Int, size: Int)
    case getKeepVoiceMessage(page: Int, size: Int)
    case sendVoiceMessage(parameters: [MultipartFormData])
    case keepVoiceMessage(messageId: Int)
    
    case deleteMessage(messageId: Int)
}

extension MessageApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .getSentTextMessage:
            return "/message/sent/text"
        
        case .getReceivedTextMessage:
            return "/message/received/text"
            
        case .getKeepTextMessage:
            return "/message/keep/text"
            
        case .sendTextMessage:
            return "/message/send/text"
            
        case .keepTextMessage(let messageId):
            return "/message/keep/text/\(messageId)"
            
        case .getSentVoiceMessage:
            return "/message/sent/voice"
            
        case .getReceivedVoiceMessage:
            return "/message/received/voice"
            
        case .getKeepVoiceMessage:
            return "/message/keep/voice"
            
        case .sendVoiceMessage:
            return "/message/send/voice"
            
        case .keepVoiceMessage(let messageId):
            return "/message/keep/voice/\(messageId)"
            
        case .deleteMessage(let messageId):
            return "/message/\(messageId)"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getSentTextMessage, .getReceivedTextMessage, .getKeepTextMessage:
            return .get
            
        case .sendTextMessage:
            return .post
        
        case .keepTextMessage:
            return .put
            
        case .getSentVoiceMessage, .getReceivedVoiceMessage, .getKeepVoiceMessage:
            return .get
            
        case .sendVoiceMessage:
            return .post
            
        case .keepVoiceMessage:
            return .put
            
        case .deleteMessage:
            return .delete
        }
    }
    
    var task: Task {
        switch self {
        case .getSentTextMessage(let page, let size),
                .getReceivedTextMessage(let page, let size),
                .getKeepTextMessage(let page, let size),
                .getSentVoiceMessage(let page, let size),
                .getReceivedVoiceMessage(let page, let size),
                .getKeepVoiceMessage(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 .sendTextMessage(let request):
            return .requestJSONEncodable(request)
            
        case .sendVoiceMessage(let parameters):
            return .uploadMultipart(parameters)
            
        case .deleteMessage:
            return .requestPlain
            
        case .keepTextMessage, .keepVoiceMessage:
            return .requestJSONEncodable(KeepMessageRequest())
        }
    }
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
    }
}
