import SignupForm from './signup.js';
import SigninForm from './signin.js';
import PasswordResetForm from './password_reset.js';
import ChangePasswordForm from './change_password.js';
import OtpEnrollForm from './otp.js';
import PoloniexController from './poloniex.js';
import PoloniexConfiguration from './account.js';
import App from './app.js';
import Api from './api.js';
import cookies from './cookies.js';
import React from 'react';
import qs from 'qs';

App.page('/signup', false, function(context) {
  if (App.isUserSignedIn()) {
    App.go('/me');
    return;
  }

  App.mount(<div>
      <SignupForm onSuccess={App.onUserSignUp}/>
    </div>);
});

App.page('/signin', false, function(context) {
  if (App.isUserSignedIn()) {
    App.go('/me');
    return;
  }

  App.mount(<div>
      <SigninForm onSuccess={App.onUserSignIn}/>
    </div>);
});

App.page('/reset-password', false, function(context) {
  if (App.isUserSignedIn()) {
    App.go('/me');
    return;
  }

  App.mount(<div>
      <PasswordResetForm />
    </div>);
});

App.page('/change-password', false, function(context) {
  if (App.isUserSignedIn()) {
    App.go('/me');
    return;
  }

  var token = qs.parse(context.querystring).token;

  if (token === undefined) {
    App.go('/');
    return;
  }

  App.mount(<div>
      <ChangePasswordForm token={token} onSuccess={App.go.bind(App, '/signin')}/>
    </div>);
});

App.page('/confirm', false, function(context) {
  var token = qs.parse(context.querystring).token;

  if (token === undefined) {
    App.go('/');
    return;
  }

  Api.Call(
    'CONFIRM_EMAIL',
    {'token': token},
    function(err, status, data) {
      if (err) {
        console.error(err, data);
      }

      App.go('/me');
    }
  );
});

App.page('/signout', true, function(context) {
  cookies.removeItem('jwt');

  App.go('/');
});

App.page('/me', true, function(context) {
  App.mount(<div>
      <PoloniexController/>
    </div>);
});

App.page('/account', true, function(context) {
  App.mount(<div>
      <PoloniexConfiguration/>
    </div>);
});

App.page('/not_confirmed', true, function(context) {
  App.mount(<div>
      <div className="row">
        <div className="box offset-3 col-6 text-center">
          <p>An email has now been sent to your email address.</p>
          <p>{'Please click the \'Confirm your account\' button to validate your email.'}</p>
        </div>
      </div>
    </div>);
});

App.page('/otp/setup', true, function(context) {
  Api.Call('OTP_ENROLL', {}, function(err, status, data) {
    if (err) {
      console.error(err, data);
      return;
    }

    App.mount(<div>
        <OtpEnrollForm onSuccess={App.onUserValidateOtp} img={'data:image/png;base64,' + data.base64img} secret={data.secret}/>
      </div>);

  });
});

App.page('/otp/validate', true, function(context) {
  App.mount(<div>
      <OtpEnrollForm onSuccess={App.onUserValidateOtp} />
    </div>);
});

App.page('/', false, function(context) {
  App.go('/me');
});

$(document).ready(function() {
  App.start();
});
