//
//  netwok.swift
//  Em-call
//
//  Created by Alex Sh on 02.03.2022.
//

import Foundation
import Alamofire

public enum HTTPMethod: String {
    case options = "OPTIONS"
    case get     = "GET"
    case head    = "HEAD"
    case post    = "POST"
    case put     = "PUT"
    case patch   = "PATCH"
    case delete  = "DELETE"
    case trace   = "TRACE"
    case connect = "CONNECT"
}
struct Post {
    var id: Int
    var title: String
    var body: String
    var userId: Int
}
public typealias HTTPHeaders = [String: String]

public protocol ParameterEncoding {
    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
   /* func request(_: URLConvertible, method: HTTPMethod, parameters: Parameters?, encoding: ParameterEncoding, headers: HTTPHeaders?)*/
}
public typealias Parameters = [String: Any]



class network  {
    var post:Post = Post(id: 0, title: "https://fixid.zolotoykod.ru", body: "", userId: 0)
     var params: [String: Any] = [
          "phone": "",
          "code": ""        ]
    var status:Int = 0
   /* init?(json: [String: Any]) {

            guard
                let id = json["id"] as? Int,
                let title = json["title"] as? String,
                let body = json["body"] as? String,
                let userId = json["userId"] as? Int
            else {
                return nil
            }

        post.id = id
        post.title = title
        post.body = body
        post.userId = userId
        }*/

    func reqPostEnter(phone:String,code:String) -> Int {
        //вход
        //var params: [String: Any]
        params["phone"] = phone
        params["code"] = code
       // var r
        request("https://fixid.zolotoykod.ru/applicant-auth/login", method: .post, parameters: params)
           // .validate()
            .responseJSON { responseJSON in
            print(responseJSON)
           
             // return r            }

        switch responseJSON.result {
        case .success(let value):
            guard
                let jsonObject = value as? [String: Any]
                else { return }
            let ret = jsonObject["statusCode"]
           // self.status = self.getArray( jsonArray: responseJSON) ?? 1
            self.status = ret as! Int
          //  print(post)

        case .failure(let error):
            print(error)
            }
        }
        return status
    }
    
        func getArray( jsonArray: Any) -> Int? {

         guard let jsonArray = jsonArray as? Array<[String: Any]> else { return nil }
        // var posts: [Post] = []

         for jsonObject in jsonArray {
           // let json = jsonObject as? [String: Any]
            let json = jsonObject
            if json["statusCode"] as! Int > 299 {
              //  if json?["statusCode"] as! Int > 299 {
            return 1
            }
         }
         return 0
     }
   /* static func getArray(from jsonArray: Any) -> [Post]? {

         guard let jsonArray = jsonArray as? Array<[String: Any]> else { return nil }
        // var posts: [Post] = []

         for jsonObject in jsonArray {
           //  if let post = Post(json: jsonObject) {
          //       posts.append(post)             }
         }
         return posts
     }*/
    func reqGet() {
        //получение списка заявителей
    request("https://fixid.zolotoykod.ru/applicant?offset=0&limit=10").responseJSON { response in
                print(response)
            }
    }
    func reqPost() {
        //получение списка заявителей
    request("https://fixid.zolotoykod.ru/applicant-auth/login").responseJSON { response in
                print(response)
            }
    }
    //request(URLConvertible, method: HTTPMethod, parameters: Parameters?, encoding: ParameterEncoding, headers: HTTPHeaders?)
   /* func fetchAllRooms(completion: ([RemoteRoom]?) -> Void) {
      Alamofire.request(
               "https://fixid.zolotoykod.ru/applicant?",
        method:.get,
        parameters: ["include_docs": "true"],
        encoding: JSONEncoding.default)
        .validate()
        .responseJSON { (response) -> Void in
          guard response.result.isSuccess else {
            print("Error while fetching remote rooms: \(response.result.error)")
            completion(nil)
            return
          }
     
          guard let value = response.result.value as? [String: AnyObject],
                let rows = value["rows"] as? [[String: AnyObject]] else {
              print("Malformed data received from fetchAllRooms service")
               completion(nil)
               return
          }
     
         // var rooms = [RemoteRoom]()
         // for roomDict in rows {    rooms.append(RemoteRoom(jsonData: roomDict))     }
     
         // completion(rooms)
      }
    }*/
    
  
    

    
    
 
         
}
