import { ajax } from 'discourse/lib/ajax';

const wizardProperties = [
  'name',
  'background',
  'save_submissions',
  'multiple_submissions',
  'after_signup',
  'after_time',
  'after_time_scheduled',
  'required'
];

const CustomWizard = Discourse.Model.extend({
  save() {
    return new Ember.RSVP.Promise((resolve, reject) => {
      const id = this.get('id');
      if (!id || !id.underscore()) return reject({ error: 'id_required' });

      let wizard = { id: id.underscore() };

      wizardProperties.forEach((p) => {
        const value = this.get(p);
        if (value) wizard[p] = value;
      });

      if (wizard['after_time'] && !wizard['after_time_scheduled']) {
        return reject({ error: 'after_time_need_time' });
      };

      const steps = this.get('steps');
      if (steps.length > 0)  {
        const stepsResult = this.buildSteps(steps);
        if (stepsResult.error) {
          reject({ error: stepsResult.error });
        } else {
          wizard['steps'] = stepsResult.steps;
        }
      }

      if (steps.length < 1 || !wizard['steps'] || wizard['steps'].length < 1) {
        return reject({ error: 'steps_required' });
      }

      ajax("/admin/wizards/custom/save", {
        type: 'PUT',
        data: {
          wizard: JSON.stringify(wizard)
        }
      }).then((result) => {
        if (result.error) {
          reject(result);
        } else {
          resolve(result);
        }
      });
    });
  },

  buildSteps(stepsObj) {
    let steps = [];
    let error = null;

    stepsObj.some((s) => {
      if (!s.id || !s.id.underscore()) {
        error = 'id_required';
        return;
      };

      let step = { id: s.id.underscore() };

      if (s.title) step['title'] = s.title;
      if (s.key) step['key'] = s.key;
      if (s.banner) step['banner'] = s.banner;
      if (s.description) step['description'] = s.description;

      const fields = s.get('fields');
      if (fields.length) {
        step['fields'] = [];

        fields.some((f) => {
          let id = f.id;

          if (!id || !id.underscore()) {
            error = 'id_required';
            return;
          }
          f.set('id', id.underscore());

          if (f.label === '') delete f.label;
          if (f.description === '') delete f.description;

          if (f.type === 'dropdown') {
            const choices = f.choices;
            if ((!choices || choices.length < 1) && !f.choices_key && !f.choices_categories) {
              error = 'field.need_choices';
              return;
            }
          }

          delete f.isNew;

          step['fields'].push(f);
        });

        if (error) return;
      }

      const actions = s.actions;
      if (actions.length) {
        step['actions'] = [];

        actions.some((a) => {
          let id = a.get('id');
          if (!id || !id.underscore()) {
            error = 'id_required';
            return;
          }

          a.set('id', id.underscore());

          delete a.isNew;

          step['actions'].push(a);
        });

        if (error) return;
      }

      steps.push(step);
    });

    if (error) {
      return { error };
    } else {
      return { steps };
    };
  },

  remove() {
    return ajax("/admin/wizards/custom/remove", {
      type: 'DELETE',
      data: {
        id: this.get('id')
      }
    }).then(() => this.destroy());
  }
});

CustomWizard.reopenClass({
  all() {
    return ajax("/admin/wizards/custom/all", {
      type: 'GET'
    }).then(result => {
      return result.wizards.map(w => CustomWizard.create(w));
    });
  },

  submissions(wizardId) {
    return ajax(`/admin/wizards/submissions/${wizardId}`, {
      type: "GET"
    }).then(result => {
      return result.submissions;
    });
  },

  create(w) {
    const wizard = this._super.apply(this);
    let steps = Ember.A();
    let props = { steps };

    if (w) {
      props['id'] = w.id;
      props['existingId'] = true;

      wizardProperties.forEach((p) => {
        props[p] = w[p];
      });

      if (w.steps && w.steps.length) {
        w.steps.forEach((s) => {
          // clean empty strings
          Object.keys(s).forEach((key) => (s[key] === '') && delete s[key]);

          let fields =  Ember.A();

          if (s.fields && s.fields.length) {
            s.fields.forEach((f) => {
              Object.keys(f).forEach((key) => (f[key] === '') && delete f[key]);

              const fieldParams = { isNew: false };
              let field = Ember.Object.create(Object.assign(f, fieldParams));

              if (f.choices) {
                let choices = Ember.A();

                f.choices.forEach((c) => {
                  choices.pushObject(Ember.Object.create(c));
                });

                field.set('choices', choices);
              }

              fields.pushObject(field);
            });
          }

          let actions = Ember.A();
          if (s.actions && s.actions.length) {
            s.actions.forEach((a) => {
              const actionParams = { isNew: false };
              const action = Ember.Object.create(Object.assign(a, actionParams));
              actions.pushObject(action);
            });
          }

          steps.pushObject(Ember.Object.create({
            id: s.id,
            key: s.key,
            title: s.title,
            description: s.description,
            banner: s.banner,
            fields,
            actions,
            isNew: false
          }));
        });
      };
    } else {
      props['id'] = '';
      props['name'] = '';
      props['background'] = '';
      props['save_submissions'] = true;
      props['multiple_submissions'] = false;
      props['after_signup'] = false;
      props['after_time'] = false;
      props['required'] = false;
      props['steps'] = Ember.A();
    };

    wizard.setProperties(props);

    return wizard;
  }
});

export default CustomWizard;