import SignupForm from './signup.js';
import SigninForm from './signin.js';
import OtpEnrollForm from './otp.js';
import PoloniexController from './poloniex.js';
import App from './app.js';
import Api from './api.js';
import cookies from './cookies.js';
import React from 'react';

class Logo extends React.Component {
  render() {
    return <div className="row">
              <div id="logo" className="offset-4 col-4">
                <a href="/">Cryptoportfolio</a>
              </div>
            </div>;
  }
}

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

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

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

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

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

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

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

App.page('/not_confirmed', true, function(context) {
  App.mount(<div>
      <Logo />
      <div className="row">
        <div className="box offset-3 col-6 text-center">
          <p>Please be patient, you account is being confirmed...</p>
          <p><a href="/me"><u>Refresh</u></a></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>
        <Logo />
        <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>
      <Logo />
      <OtpEnrollForm onSuccess={App.onUserValidateOtp} />
    </div>);
});

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

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