import { moment } from 'meteor/momentjs:moment';

const options = { sort: { actual: -1 }, limit: H.AMOUNT_SHOWN };
const not_found = new ReactiveVar(false);
const not_found_text = `
  <p><strong>The harbor you're viewing hasn't been installed for this
    Harbormaster instance.</strong></p>
  <p>Editing it has been disabled.  To enable it, the harbor will need to
    be installed in the Harbormaster harbor directory
    (<code>~/.harbormaster/harbors</code> by default).</p>
`;

Template.edit_lane.onCreated(function () {
  this.autorun(() => {
    const name = FlowRouter.getParam('name');
    const lane = get_lane(name);

    if (lane) this.subscribe('Shipments', lane, options);
  });
});

Template.edit_lane.onRendered(function () {
  const lane = get_lane(FlowRouter.getParam('name'));
  const harbor = lane && Harbors.findOne(lane.type) || {};
  // https://github.com/meteor/blaze/issues/205#issuecomment-305156895
  if (lane) $('#rendered-input').html(lane.rendered_input);
  else $('#rendered-input').html(harbor.rendered_input);
});

Template.edit_lane.helpers({
  lane_name () {
    var name = FlowRouter.getParam('name');
Template.edit_lane.events({
  'submit form': function submit_form (event, template) {
    event.preventDefault();

    let lane = get_lane(FlowRouter.getParam('name')) || Session.get('lane');

    if (
      lane.name &&
      lane.name != 'New' &&
      lane.type
    ) {
      Session.set('validating_fields', true);

      return update_harbor(template);
    }

    return lane;
  },

  'change .followup': function change_followup_lane (event) {
    let lane = get_lane(FlowRouter.getParam('name'));
    let followup_lane = Lanes.findOne(event.target.value);

    if (
      lane.name &&
      lane.name != 'New' &&
      lane.type
    ) {
      lane.followup = followup_lane ? followup_lane._id : null;
      return update_lane(lane);
    }
    return lane;
  },

  'change .salvage-plan': function change_salvage_plan (event) {
    let lane = get_lane(FlowRouter.getParam('name'));
    let salvage_plan_lane = Lanes.findOne(event.target.value);

    if (
      lane.name &&
      lane.name != 'New' &&
      lane.type
    ) {
      lane.salvage_plan = salvage_plan_lane ? salvage_plan_lane._id : null;
      return update_lane(lane);
    }
  },

  'change .lane-name': function change_lane_name (event) {
    let lane = Session.get('lane') || {};
    lane.name = event.target.value;
    if (Lanes.findOne(lane._id)) update_lane(lane);
    else Session.set('lane', lane);
    FlowRouter.go('/lanes/' + lane.name + '/edit');
  },

  'keypress .lane-name': function (event) {
    if (event.key == 'Enter') event.preventDefault();
  },

  'change .captains': function change_captains (event) {
    let lane = get_lane(FlowRouter.getParam('lane'));
    let captains = lane.captains || [];
    let user = event.target.value;

    if (event.target.checked) {
      captains.push(user);
    }
    else {
      captains = _.reject(captains, function remove_captain (captain) {
        return captain == user;
      });
    }
    lane.captains = captains;

    if (Lanes.findOne(lane._id)) update_lane(lane);
  },

  'click .add-harbor': function add_destination (event) {
    event.preventDefault();

    return Session.set('choose_type', true);
  },

  'click .back-to-lanes': function back_to_lanes (event) {
    event.preventDefault();

    Session.set('lane', null);
    return FlowRouter.go('/lanes');
  },

  'click .choose-harbor-type': function choose_harbor_type (event) {
    event.preventDefault();

    let type = $(event.target).attr('data-type');
    let lane = Session.get('lane');

    lane.type = type;
    return H.call('Lanes#upsert', lane, (err, res) => {
      if (err) throw err;
      console.log(`Lane ${lane.name} added: ${res}`);
      Session.set('lane', lane);
      return res;
    });
  },

  'click .add-followup': function add_followup_lane (e) {
    e.preventDefault();
    return Session.set('choose_followup', true);
  },

  'click .add-salvage-plan': function add_salvage_plan (e) {
    e.preventDefault();
    return Session.set('choose_salvage_plan', true);
  },

  'click .new-lane': function new_lane () {
    Session.set('lane', {});
  },

});