import React from 'react';
import Config from '../../config/appConstants';
class FormService {
  constructor() {

  }
  async retrieveTemplate() {
    return fetch(Config.LANDTEMPLATEDATA)
      .then(response => {
        if (!response.ok) {
          this.handleResponseError(response);
        }
        return response.json();
      })
      .then(json => {
      
       // console.log(json);
         const items = [];
         const result = [];

         for (const [index, value] of json.entries()) {
            //items.push(<li key={index}>{value}</li>)
            items.push(value);
          }

        // const itemArray = json._embedded.collectionItems;
        // for(var i = 0; i < itemArray.length; i++) {
        //   itemArray[i]["link"] =  itemArray[i]._links.self.href;
        //   items.push(itemArray[i]);
        // }
        //items.push(item.parseJSON)
        
        //json.map((item) => ( items.push(item)));

        //Object.keys(json).forEach(function(key) {
          //  console.log('-------', key);
            //items.push(json[key]);
            // Object.keys(json[key]).forEach(function(index,val) {
            // console.log('key', index);
            // items[key] = index;
            // });
         // });
       
        //console.log('----------', result);
        return items;
      })
      .catch(error => {
        this.handleError(error);
      });
  }
  async getItem(itemLink) {
    console.log("ItemService.getItem():");
    console.log("Item: " + itemLink);
    return fetch(itemLink)
      .then(response => {
        if (!response.ok) {
            this.handleResponseError(response);
        }
        return response.json();
      })
      .then(item => {
          item["link"] = item._links.self.href;
          return item;
        }
      )
      .catch(error => {
        this.handleError(error);
      });
  }
  async createItem(newitem) {
    console.log("ItemService.createItem():");
    console.log(newitem);
    return fetch(Config.ITEM_COLLECTION_URL, {
      method: "POST",
      mode: "cors",
      headers: {
            "Content-Type": "application/json"
        },
      body: JSON.stringify(newitem)
    })
      .then(response => {
       if (!response.ok) {
            this.handleResponseError(response);
        }
        return response.json();
      })
      .catch(error => {
        this.handleError(error);
      });
  }
  async deleteItem(itemlink) {
    console.log("ItemService.deleteItem():");
    console.log("item: " + itemlink);
    return fetch(itemlink, {
      method: "DELETE",
      mode: "cors"
    })
      .then(response => {
        if (!response.ok) {
            this.handleResponseError(response);
        }
      })
      .catch(error => {
        this.handleError(error);
      });
  }
  async updateItem(item) {
    console.log("ItemService.updateItem():");
    console.log(item);
    return fetch(item.link, {
      method: "PUT",
      mode: "cors",
      headers: {
            "Content-Type": "application/json"
          },
      body: JSON.stringify(item)
    })
      .then(response => {
        if (!response.ok) {
          this.handleResponseError(response);
        }
        return response.json();
      })
      .catch(error => {
        this.handleError(error);
      });
  }
  handleResponseError(response) {
      throw new Error("HTTP error, status = " + response.status);
  }
  handleError(error) {
      console.log(error.message);
  }

  
}
export default FormService;