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

import Foundation
import Moya

enum CanApi {
    case getCanStatus
    case getCanChargeStatus
    case getCanUseStatus
    case getCans
    
    case chargeCan(request: CanChargeRequest)
    case verify(request: CanVerifyRequest)
    
    case pgChargeCan(request: PgChargeRequest)
    case pgVerify(request: PgVerifyRequest)
    case pgVerifyHecto(request: PgVerifyRequest)
    
    case useCanCoupon(request: UseCanCouponRequest)
}

extension CanApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .getCanStatus:
            return "/can/status"
            
        case .getCanChargeStatus:
            return "/can/status/charge"
            
        case .getCanUseStatus:
            return "/can/status/use"
            
        case .getCans:
            return "/can"
            
        case .chargeCan:
            return "/charge/apple"
            
        case .verify:
            return "/charge/apple/verify"
            
        case .pgChargeCan:
            return "/charge"
            
        case .pgVerify:
            return "/charge/verify"
            
        case .pgVerifyHecto:
            return "/charge/verify/hecto"
            
        case .useCanCoupon:
            return "/can/coupon/use"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .getCanStatus, .getCanChargeStatus, .getCanUseStatus, .getCans:
            return .get
            
        case .chargeCan, .verify, .pgChargeCan, .pgVerify, .pgVerifyHecto, .useCanCoupon:
            return .post
        }
    }
    
    var task: Task {
        switch self {
        case .getCanStatus:
            return .requestParameters(
                parameters: ["container" : "ios"],
                encoding: URLEncoding.queryString
            )
            
        case .getCanChargeStatus, .getCanUseStatus:
            return .requestParameters(
                parameters: [
                    "timezone": TimeZone.current.identifier,
                    "page": 0,
                    "size": 30,
                    "container": "ios"
                ],
                encoding: URLEncoding.queryString
            )
            
        case .getCans:
            return .requestPlain
            
        case .chargeCan(let request):
            return .requestJSONEncodable(request)
            
        case .verify(let request):
            return .requestJSONEncodable(request)
            
        case .pgChargeCan(let request):
            return .requestJSONEncodable(request)
            
        case .pgVerify(let request):
            return .requestJSONEncodable(request)
            
        case .pgVerifyHecto(let request):
            return .requestJSONEncodable(request)
            
        case .useCanCoupon(let request):
            return .requestJSONEncodable(request)
        }
    }
    
    var headers: [String : String]? {
        switch self {
        default:
            return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
        }
    }
}
