//
//  UserApi.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import Foundation
import Moya

enum UserApi {
    case login(request: LoginRequest)
    case signUp(parameters: [MultipartFormData])
    case findPassword(request: ForgotPasswordRequest)
    case searchUser(nickname: String)
    case getMypage
    case getMemberInfo
    case notification(request: UpdateNotificationSettingRequest)
    case logout
    case logoutAllDevice
    case signOut(request: SignOutRequest)
    case updatePushToken(request: PushTokenUpdateRequest)
    case creatorFollow(request: CreatorFollowRequest)
    case creatorUnFollow(request: CreatorFollowRequest)
    case getBlockedMemberIdList
    case getBlockedMemberList(page: Int, size: Int)
    case memberBlock(request: MemberBlockRequest)
    case memberUnBlock(request: MemberBlockRequest)
    case getMyProfile
    case profileImageUpdate(parameter: MultipartFormData)
    case profileUpdate(request: ProfileUpdateRequest)
    case getChangeNicknamePrice
    case checkNickname(nickname: String)
    case changeNickname(request: ProfileUpdateRequest)
    case updateIdfa(request: IdfaUpdateRequest)
    case getMemberProfile(memberId: Int)
}

extension UserApi: TargetType {
    var baseURL: URL {
        return URL(string: BASE_URL)!
    }
    
    var path: String {
        switch self {
        case .login:
            return "/member/login"
            
        case .signUp:
            return "/member/signup"
            
        case .findPassword:
            return "/forgot-password"
            
        case .searchUser:
            return "/member/search"
            
        case .getMypage:
            return "/member/mypage"
            
        case .getMemberInfo:
            return "/member/info"
            
        case .notification:
            return "/member/notification"
            
        case .logout:
            return "/member/logout"
            
        case .logoutAllDevice:
            return "/member/logout/all"
            
        case .signOut:
            return "/member/sign_out"
            
        case .updatePushToken:
            return "/member/push-token/update"
            
        case .creatorFollow:
            return "/member/creator/follow"
            
        case .creatorUnFollow:
            return "/member/creator/unfollow"
            
        case .getBlockedMemberIdList:
            return "/member/block/id"
            
        case .getBlockedMemberList, .memberBlock:
            return "/member/block"
            
        case .memberUnBlock:
            return "/member/unblock"
            
        case .getMyProfile, .profileUpdate:
            return "/member"
            
        case .profileImageUpdate:
            return "/member/image"
            
        case .getChangeNicknamePrice:
            return "/member/change/nickname/price"
            
        case .checkNickname:
            return "/member/check/nickname"
            
        case .changeNickname:
            return "/member/change/nickname"
            
        case .updateIdfa:
            return "/member/adid/update"
            
        case .getMemberProfile(let memberId):
            return "/member/profile/\(memberId)"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .login, .signUp, .findPassword, .notification, .logout, .logoutAllDevice, .signOut, .creatorFollow, .creatorUnFollow, .memberBlock, .memberUnBlock,
                .profileImageUpdate:
            return .post
            
        case .searchUser, .getMypage, .getMemberInfo, .getMyProfile, .getChangeNicknamePrice, .checkNickname, .getBlockedMemberList, .getBlockedMemberIdList, .getMemberProfile:
            return .get
            
        case .updatePushToken, .profileUpdate, .changeNickname, .updateIdfa:
            return .put
        }
    }
    
    var task: Task {
        switch self {
        case .login(let request):
            return .requestJSONEncodable(request)
            
        case .signUp(let parameters):
            return .uploadMultipart(parameters)
            
        case .findPassword(let request):
            return .requestJSONEncodable(request)
            
        case .searchUser(let nickname):
            return .requestParameters(parameters: ["nickname" : nickname], encoding: URLEncoding.queryString)
            
        case .getMypage, .getMyProfile, .getMemberInfo:
            return .requestParameters(parameters: ["container" : "ios"], encoding: URLEncoding.queryString)
            
        case .logout, .logoutAllDevice, .getChangeNicknamePrice, .getBlockedMemberIdList, .getMemberProfile:
            return .requestPlain
            
        case .notification(let request):
            return .requestJSONEncodable(request)
            
        case .signOut(let request):
            return .requestJSONEncodable(request)
            
        case .updatePushToken(let request):
            return .requestJSONEncodable(request)
            
        case .creatorFollow(let request):
            return .requestJSONEncodable(request)
            
        case .creatorUnFollow(let request):
            return .requestJSONEncodable(request)
            
        case .getBlockedMemberList(let page, let size):
            let parameters = ["page": page - 1, "size": size] as [String : Any]
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
            
        case .memberBlock(let request):
            return .requestJSONEncodable(request)
            
        case .memberUnBlock(let request):
            return .requestJSONEncodable(request)
            
        case .profileImageUpdate(let parameter):
            return .uploadMultipart([parameter])
            
        case .profileUpdate(let request):
            return .requestJSONEncodable(request)
            
        case .checkNickname(let nickname):
            return .requestParameters(parameters: ["nickname" : nickname], encoding: URLEncoding.queryString)
            
        case .changeNickname(let request):
            return .requestJSONEncodable(request)
            
        case .updateIdfa(let request):
            return .requestJSONEncodable(request)
        }
    }
    
    var headers: [String : String]? {
        switch self {
        case .login, .signUp, .findPassword:
            return nil
            
        default:
            return ["Authorization": "Bearer \(UserDefaults.string(forKey: UserDefaultsKey.token))"]
        }
    }
}
