import fetch from "isomorphic-fetch";
import {appConfig} from "../config/app";

export class Http {
  token = '';
  baseUrl = '';

  constructor(token, baseUrl = appConfig.apiUrl) {
    if (token) {
      this.token = token;
    }

    this.baseUrl = baseUrl;
  }

  checkStatus(result) {
    if (!result.ok) {
      return result.json().then(data => {
        return Promise.reject(data);
      });
    }
    return result;
  }

  raw(url, options={}) {
    let headers = new Headers();
    console.log(url, options, "raw http request");
    return fetch(url, {headers: {}})
      .then(response => {
        if (options.raw){
          return response;
        }

        return response.json();
      })
      .catch(err => {
        console.log(err);
        throw err;
      })
  }

  get(url, options={}) {
    let headers = new Headers();
    headers.append("Authorization", `Bearer ${this.token}`);

    return fetch(this.baseUrl + url, Object.assign(options, {headers}))
      .then(this.checkStatus)
      .then(response => {
        if (options.raw){
          return response;
        }

        return response.json();
      })
      .catch(err => {
        console.log(err);
        throw err;
      })
  }

  post(url, data, options={}) {
    let headers = new Headers();
    headers.append("Authorization", `Bearer ${this.token}`);
    headers.append("Content-Type", "application/json");

    return fetch(this.baseUrl + url, Object.assign({
      method: 'POST',
      headers,
      body: JSON.stringify(data)
    }, options))
      .then(this.checkStatus)
      .then(response => {
        if (options.raw){
          return response;
        }

        return response.json()
      })
      .catch(err => {
        console.log(err);
        throw err;
      })
  }

  put(url, data, options={}) {
    let headers = new Headers();
    headers.append("Authorization", `Bearer ${this.token}`);
    headers.append("Content-Type", "application/json");

    return fetch(this.baseUrl + url, Object.assign({
      method: 'PUT',
      headers,
      body: JSON.stringify(data)
    }, options))
      .then(this.checkStatus)
      .then(response => {
        if (options.raw){
          return response;
        }

        return response.json()
      })
      .catch(err => {
        console.log(err);
        throw err;
      })
  }

  delete(url, options={}) {
    let headers = new Headers();
    headers.append("Authorization", `Bearer ${this.token}`);

    return fetch(this.baseUrl + url, {
      headers,
      method: 'DELETE'
    })
      .then(this.checkStatus)
      .then(response => {
        if (options.raw){
          return response;
        }

        return response.json()
      })
      .catch(err => {
        console.log(err);
        throw err;
      })
  }

  upload(file, options={}) {
    let headers = new Headers();
    headers.append("Authorization", `Bearer ${this.token}`);
    let data = new FormData();
    data.append('file', file);

    return fetch(appConfig.apiUrl + 'files', Object.assign({
      method: 'POST',
      headers,
      body: data
    }, options))
      .then(this.checkStatus)
      .then(response => {
        if (options.raw){
          return response;
        }

        return response.json()
      })
      .catch(err => {
        console.log(err);
        throw err;
      })
  }


  appendImagePath(path) {
    return appConfig.imageUrl + path;
  }

  // static appendImagePath(path) {
  //   return appConfig.imageUrl + path;
  // }
}
