import { Component, Event, h, Listen, Prop, State, Watch } from '@stencil/core';
import { StateOfReminder } from '../../utils/utils';
import { DateTime } from 'luxon';
export class AddonReminder {
  constructor() {
    this.userInfo = { name: 'Nome', surname: 'Cognome', date: 'creato oggi', image: '' };
    this.state = '';
    this.taskId = 722;
    this.idPluginAction = 9;
    this.baseUrl = 'https://app.dev.synbee.it';
    this.idPluginActionExternal = 22;
    this.badgeErrorOn = false;
    this.isFirstTime = true;
    this.reminderInfo = {
      name: 'Promemoria',
      description: 'Descrizione',
      hour: '01',
      minute: '01',
      duration: '00',
      date: '01/01/2021'
    };
    this.reminderDone = false;
    this.status = StateOfReminder.create;
  }
  // @Watch('reminderInfo')
  // updateReminderInfo(){
  //   this.reminderInfoState = this.reminderInfo;
  // }
  catchUndoReminder() {
    if (this.status === StateOfReminder.edit) {
      this.status = StateOfReminder.view;
    }
    else {
      this.undoReminderOutEmitter.emit(true);
    }
  }
  catchCloseAction(ev) {
    this.collapseReminder.emit(ev.detail);
  }
  sendOutputData(ev) {
    let outputToChange = Object.assign({}, ev.detail);
    let arrayDate = ev.detail.date.split('/');
    let month;
    if (arrayDate[1].startsWith('0')) {
      month = parseInt(arrayDate[1].replace('0', ''));
      month = month - 1;
    }
    else {
      month = parseInt(arrayDate[1]);
      month = month - 1;
    }
    let newHour = parseInt(ev.detail.hour.startsWith('0') ? ev.detail.hour.replace('0', '') : ev.detail.hour) + parseInt(ev.detail.duration.startsWith('0') ? ev.detail.duration.replace('0', '') : ev.detail.duration);
    outputToChange.dateTime = new Date(arrayDate[2], month, arrayDate[0], ev.detail.hour.startsWith('0') ? ev.detail.hour.replace('0', '') : ev.detail.hour, ev.detail.minute);
    outputToChange.dateTimeEnd = new Date(arrayDate[2], month, arrayDate[0], newHour, ev.detail.minute);
    let startDate = DateTime.fromJSDate(outputToChange.dateTime).toISO({ includeOffset: false });
    let endDate = DateTime.fromJSDate(outputToChange.dateTimeEnd).toISO({ includeOffset: false });
    let body = { name: outputToChange.name, description: outputToChange.description, startDate: startDate, endDate: endDate };
    fetch(`${this.baseUrl}/services/eolo-api-gateway/external-api/${this.taskId}/${this.idPluginAction}`, {
      method: 'POST',
      body: JSON.stringify(body),
      headers: {
        "Content-Type": "application/json"
      }
    })
      .then((response) => response.json())
      .then(response => {
      this.outputDataEmitter.emit(response);
    });
  }
  editOutputData(ev) {
    let outputToChange = Object.assign({}, ev.detail);
    let arrayDate = ev.detail.date.split('/');
    let month;
    if (arrayDate[1].startsWith('0')) {
      month = parseInt(arrayDate[1].replace('0', ''));
      month = month - 1;
    }
    else {
      month = parseInt(arrayDate[1]);
      month = month - 1;
    }
    let newHour = parseInt(ev.detail.hour.startsWith('0') ? ev.detail.hour.replace('0', '') : ev.detail.hour) + parseInt(ev.detail.duration.startsWith('0') ? ev.detail.duration.replace('0', '') : ev.detail.duration);
    outputToChange.dateTime = new Date(arrayDate[2], month, arrayDate[0], ev.detail.hour.startsWith('0') ? ev.detail.hour.replace('0', '') : ev.detail.hour, ev.detail.minute);
    outputToChange.dateTimeEnd = new Date(arrayDate[2], month, arrayDate[0], newHour, ev.detail.minute);
    let startDate = DateTime.fromJSDate(outputToChange.dateTime).toISO({ includeOffset: false });
    let endDate = DateTime.fromJSDate(outputToChange.dateTimeEnd).toISO({ includeOffset: false });
    let body = { name: outputToChange.name, description: outputToChange.description, startDate: startDate, endDate: endDate };
    fetch(`${this.baseUrl}/services/eolo-api-gateway/external-api/${this.idPluginActionExternal}${this.idPluginAction}`, {
      method: 'PUT',
      body: JSON.stringify(body)
    })
      .then((response) => response.json())
      .then(response => {
      this.editOutputDataEmitter.emit(response);
    });
  }
  catchErrorFromUncomplitedDate(ev) {
    this.badgeErrorOn = ev.detail === true;
  }
  setReminderDone(ev) {
    this.reminderDone = ev.detail;
    this.reminderDoneSetter.emit(this.reminderDone);
  }
  changeStat(ev) {
    if (ev.detail === 'edit') {
      this.status = StateOfReminder.edit;
    }
    else {
      fetch(`${this.baseUrl}/services/eolo-api-gateway/external-api/${this.idPluginActionExternal}${this.idPluginAction}`, {
        method: 'DELETE'
      })
        .then((response) => response.json())
        .then(response => {
        console.log(response);
        this.deleteAddonReminderEmitter.emit(true);
      });
    }
  }
  createReminder() {
    this.status = StateOfReminder.view;
  }
  takeInfoFromBody(ev) {
    let reminder = Object.assign({}, this.reminderInfo);
    if (ev.detail.hour) {
      reminder.hour = ev.detail.hour;
    }
    if (ev.detail.minute) {
      reminder.minute = ev.detail.minute;
    }
    if (ev.detail.duration) {
      reminder.duration = ev.detail.duration;
    }
    if (ev.detail.description) {
      reminder.description = ev.detail.description;
    }
    if (ev.detail.date) {
      reminder.date = ev.detail.date;
    }
    this.reminderInfo = Object.assign({}, reminder);
  }
  takeInfoFromHeder(ev) {
    let reminder = Object.assign({}, this.reminderInfo);
    reminder.name = ev.detail;
    this.reminderInfo = Object.assign({}, reminder);
  }
  stateDetectionChange() {
    if (this.state === 'view') {
      this.status = StateOfReminder.view;
    }
  }
  componentWillRender() {
    if (this.isFirstTime) {
      if (this.state) {
        if (this.state === 'view') {
          this.status = StateOfReminder.view;
        }
        else if (this.state === 'create') {
          this.status = StateOfReminder.create;
          this.reminderInfo.description = '';
        }
        else {
          this.status = StateOfReminder.edit;
        }
      }
      else {
        this.status = StateOfReminder.create;
      }
      this.isFirstTime = false;
    }
  }
  validationOfData(newValue) {
    if (newValue.hour && newValue.minute && newValue.duration && newValue.description && newValue.date && newValue.name) {
      this.badgeErrorOn = false;
      this.validation = true;
    }
    else {
      this.validation = false;
    }
  }
  render() {
    return (h("div", { class: "reminder-container" },
      h("addon-reminder-header", { addonReminderTitle: this.reminderInfo.name, creationDate: this.reminderInfo.date, state: this.status }),
      h("addon-reminder-body", { date: this.reminderInfo.date, reminderDoneValue: this.reminderDone, addonReminderInfoFromParent: this.reminderInfo, state: this.status, descriptionText: this.reminderInfo.description }),
      h("addon-reminder-footer", { reminderDoneProp: this.reminderDone, author: this.userInfo, submitValidation: this.validation, reminderAddonInfo: this.reminderInfo, state: this.status }),
      h("div", { class: { 'badge-error': true, 'hidden': !this.badgeErrorOn } },
        h("synbee-badge-error", null))));
  }
  static get is() { return "addon-reminder"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["addon-reminder.css"]
  }; }
  static get styleUrls() { return {
    "$": ["addon-reminder.css"]
  }; }
  static get properties() { return {
    "userInfo": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "AuthorStencilDTO",
        "resolved": "AuthorStencilDTO",
        "references": {
          "AuthorStencilDTO": {
            "location": "import",
            "path": "../../interfaces"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{name: 'Nome', surname:'Cognome', date:'creato oggi', image:''}"
    },
    "state": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "state",
      "reflect": false,
      "defaultValue": "''"
    },
    "taskId": {
      "type": "number",
      "mutable": true,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "task-id",
      "reflect": false,
      "defaultValue": "722"
    },
    "idPluginAction": {
      "type": "number",
      "mutable": true,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "id-plugin-action",
      "reflect": false,
      "defaultValue": "9"
    },
    "baseUrl": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "base-url",
      "reflect": false,
      "defaultValue": "'https://app.dev.synbee.it'"
    },
    "idPluginActionExternal": {
      "type": "number",
      "mutable": true,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "id-plugin-action-external",
      "reflect": false,
      "defaultValue": "22"
    },
    "reminderInfo": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "{\n    name: string;\n    description: string;\n    hour: string;\n    minute: string;\n    duration: string;\n    date: string;\n  }",
        "resolved": "{ name: string; description: string; hour: string; minute: string; duration: string; date: string; }",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{\n    name: 'Promemoria',\n    description: 'Descrizione',\n    hour: '01',\n    minute: '01',\n    duration: '00',\n    date: '01/01/2021'\n  }"
    }
  }; }
  static get states() { return {
    "reminderInfoState": {},
    "reminderDone": {},
    "status": {}
  }; }
  static get events() { return [{
      "method": "undoReminderOutEmitter",
      "name": "undoReminderOutEmitter",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }, {
      "method": "outputDataEmitter",
      "name": "outputDataEmitter",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "AddonReminderStencilDTO",
        "resolved": "AddonReminderStencilDTO",
        "references": {
          "AddonReminderStencilDTO": {
            "location": "import",
            "path": "../../interfaces"
          }
        }
      }
    }, {
      "method": "deleteAddonReminderEmitter",
      "name": "deleteAddonReminderEmitter",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }, {
      "method": "collapseReminder",
      "name": "collapseReminder",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }, {
      "method": "reminderDoneSetter",
      "name": "reminderDoneSetter",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }, {
      "method": "editOutputDataEmitter",
      "name": "editOutputDataEmitter",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "AddonReminderStencilDTO",
        "resolved": "AddonReminderStencilDTO",
        "references": {
          "AddonReminderStencilDTO": {
            "location": "import",
            "path": "../../interfaces"
          }
        }
      }
    }]; }
  static get watchers() { return [{
      "propName": "state",
      "methodName": "stateDetectionChange"
    }, {
      "propName": "reminderInfo",
      "methodName": "validationOfData"
    }]; }
  static get listeners() { return [{
      "name": "undoReminderEmitter",
      "method": "catchUndoReminder",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "closeReminderEmitter",
      "method": "catchCloseAction",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "createReminderEmitterFromSubmit",
      "method": "sendOutputData",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "editReminderEmitterFromSubmit",
      "method": "editOutputData",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "errorEmitter",
      "method": "catchErrorFromUncomplitedDate",
      "target": undefined,
      "capture": false,
      "passive": false
    }, {
      "name": "reminderDoneEmitter",
      "method": "setReminderDone",
      "target": undefined,
      "capture": true,
      "passive": true
    }, {
      "name": "changeStatusToAddonReminder",
      "method": "changeStat",
      "target": undefined,
      "capture": true,
      "passive": true
    }, {
      "name": "createReminderEmitter",
      "method": "createReminder",
      "target": undefined,
      "capture": true,
      "passive": true
    }, {
      "name": "addonReminderInfoFromBodyEmitter",
      "method": "takeInfoFromBody",
      "target": undefined,
      "capture": true,
      "passive": true
    }, {
      "name": "addonReminderInfoFromHeaderEmitter",
      "method": "takeInfoFromHeder",
      "target": undefined,
      "capture": true,
      "passive": true
    }]; }
}
