/*
 * Module dependencies.
 */

var citizen = require('citizen');
var dom = require('dom');
var FormView = require('form-view');
var template = require('./template');
var page = require('page');
var t = require('t');

/**
 * Expose SigninForm.
 */

module.exports = SigninForm;

/**
 * Signin SigninForm
 *
 * @return {SigninForm} `SigninForm` instance.
 * @api public
 */

function SigninForm () {
  if (!(this instanceof SigninForm)) {
    return new SigninForm();
  }

  FormView.call(this, template);
}

/**
 * Inherit from `FormView`
 */

FormView(SigninForm);

SigninForm.prototype.switchOn = function () {
  this.on('success', this.bound('onsuccess'));
};

/**
 * Show success message
 */
SigninForm.prototype.onsuccess = function () {
  citizen.load('me');
  var sidebarItem = dom('.nav-proposal .sidebar-nav .nav.navlist li.active');
  if (sidebarItem.length) {
    var id = sidebarItem.attr('data-id');
    page('/law/' + id);
  } else {
    page('/');
  }
};

/**
 * Handle http response to show message to the user.
 *
 * @param err
 * @param res
 * @returns {*}
 * @override from {FormView}
 */
SigninForm.prototype.response = function (err, res) {
  if (!res.ok) {
    return this.errors([JSON.parse(res.text).error]);
  }

  //Redirect if come from unverified email
  //FIXME: this error detection mechanism is a little weird, we should avoid compare keys.
  if (res.ok && JSON.parse(res.text).error === t("signin.error.email-not-valid")) {
    page('/signup/resend-validation-email');
  }

  if (err || (res.body && res.body.error)) {
    return this.errors([err || res.body.error]);
  }

  this.emit('success', res);
};

/*
 * Module dependencies.
 */

var citizen = require('citizen');
var dom = require('dom');
var FormView = require('form-view');
var template = require('./template');
var page = require('page');
var t = require('t');

/**
 * Expose SigninForm.
 */

module.exports = SigninForm;

/**
 * Signin SigninForm
 *
 * @return {SigninForm} `SigninForm` instance.
 * @api public
 */

function SigninForm () {
  if (!(this instanceof SigninForm)) {
    return new SigninForm();
  }

  FormView.call(this, template);
}

/**
 * Inherit from `FormView`
 */

FormView(SigninForm);

SigninForm.prototype.switchOn = function () {
  this.on('success', this.bound('onsuccess'));
};

/**
 * Show success message
 */
SigninForm.prototype.onsuccess = function () {
  citizen.load('me');
  var sidebarItem = dom('.nav-proposal .sidebar-nav .nav.navlist li.active');
  if (sidebarItem.length) {
    var id = sidebarItem.attr('data-id');
    page('/law/' + id);
  } else {
    page('/');
  }
};

/**
 * Handle http response to show message to the user.
 *
 * @returns {Mixed}
 * @override from {FormView}
 */
SigninForm.prototype.response = function (err, res) {
  if (err) {
    return this.errors([err]);
  }

  if (!res.ok) {
    return this.errors([res.text]);
  }

  //Redirect if come from unverified email
  //FIXME: this error detection mechanism is a little weird, we should avoid compare keys.
  if (res.ok && JSON.parse(res.text).error === t('signin.error.email-not-valid')) {
    page('/signup/resend-validation-email');
  }

  if (res.body && res.body.error) {
    return this.errors([res.body.error]);
  }

  this.emit('success', res);
};

Update error handling on response override to match refactored approach in FormView