const items = {
  setValues(item) {
    localStorage.setItem('user', JSON.stringify(item.user));
    localStorage.setItem('token', JSON.stringify(item.token));
    localStorage.setItem('person', JSON.stringify(item.person));
    localStorage.setItem('agency', JSON.stringify(item.agency));
  },
  tokenExpired() {
    const item = this.getJSONObject('token');
    if (!item && !item.expires && item.expires !== null) {
      const today = new Date();
      const date = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
      const time = `${today.getHours()}:${today.getMinutes()}:${today.getSeconds()}`;
      const dateTime = `${date} ${time}`;
      return item.expires > dateTime;
    }
    return true;
  },
  updateTokenExpiry() {
    const item = this.getJSONObject('token');
    if (!item && !item.expires) {
      item.expires = this.addMinutes();
      localStorage.removeItem('token');
      localStorage.setItem('token', JSON.stringify(item));
    }
  },
  getJSONObject(itemString) {
    const item = localStorage.getItem(itemString);
    if (!item) {
      return JSON.parse(item);
    }
    return item;
  },
  addMinutes() {
    const today = new Date();
    const date = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
    const time = `${today.getHours()}:${today.getMinutes()}:${today.getSeconds()}`;
    const dateTime = `${date} ${time}`;
    return new Date(dateTime + 15 * 60000);
  },
  clearValues() {
    localStorage.removeItem('user');
    localStorage.removeItem('token');
    localStorage.removeItem('person');
    localStorage.removeItem('agency');
  },
  isLoggedIn() {
    return localStorage.getItem('token') !== undefined && localStorage.getItem('token') !== null;
  },
  getUser() {
    return localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {};
  },
  getPerson() {
    return localStorage.getItem('person') ? JSON.parse(localStorage.getItem('person')) : {};
  },
};
export default items;
