//
//  SinglePointAPI.swift
//  MobilePortal
//
//  Created by caiyang on 2020/7/29.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit
import Moya
import HandyJSON
import SwiftyJSON

let SinglePointProvider = MoyaProvider<SinglePointAPI>()

enum SinglePointAPI {
    case singlePointAccessToken(userName:String,password:String)
    case singlePointIsAccessToken(value:String)
    case singlePointGetUserInfo(value:String)
}

extension SinglePointAPI: TargetType {
    var baseURL: URL {
        if AccountInfo.sharedInstance().isTest == "1" {
            return URL(string: "http://222.66.127.246:8020/Single/authentication")!
        }
        return URL(string: "http://222.66.127.246:8020")!
    }
    
    var path: String {
        switch self {
        case .singlePointAccessToken: return ""
        case .singlePointIsAccessToken: return "/auth"
        case .singlePointGetUserInfo: return "/auth"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var sampleData: Data { return "".data(using: String.Encoding.utf8)! }
    var headers: [String : String]? {
        return ["Content-type":"application/json;charset=utf-8"]
    }
    
    var task: Task {
        var parmeters:[String:Any] = [:]
        switch self {
        case .singlePointAccessToken(let userName, let password):
            parmeters["appId"] = "K2"
            parmeters["userName"] = userName
            parmeters["passWord"] = password
            parmeters["remoteIp"] = AccountInfo.sharedInstance().singlePoint_remoteIp
            parmeters["authnMethod"] = "UsernamePassword"
        case .singlePointIsAccessToken( _):
            parmeters["tokenId"] = AccountInfo.sharedInstance().singlePoint_access_token
            parmeters["appid"] = AccountInfo.sharedInstance().weChat_appid
            parmeters["remoteIp"] = ""
        case .singlePointGetUserInfo( _):
            parmeters["appid"] = AccountInfo.sharedInstance().weChat_appid
            parmeters["tokenId"] = AccountInfo.sharedInstance().singlePoint_access_token
            parmeters["remoteIp"] = ""
            parmeters["attributeNames"] = ""
        }
        print(parmeters)
        //        return .requestData(jsonToData(jsonDic: parmeters)!)
        return .requestParameters(parameters: parmeters, encoding: URLEncoding.default)
    }
}
