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 App from './app.js';
import Api from './api.js';
import cookies from './cookies.js';
import React from 'react';
import qs from 'qs';

class Header extends React.Component {
  render = () => {
    if (this.props.displayLogout === true) {
      return <div id="header" className="row">
                <div className="offset-4 col-4">
                  <Logo />
                </div>
                <div className="offset-2 col-2 align-self-center h-100">
                  <a href="/signout"><u>Logout</u></a>
                </div>
              </div>;
    }
      return <div id="header" className="row">
                <div className="offset-4 col-4">
                  <Logo />
                </div>
              </div>;

  }
}

class Logo extends React.Component {
  render() {
    return <div id="logo" className="w-100 h-100 align-self-center">
             <h1><a href="/">CryptoPF</a></h1>
           </div>;
  }
}

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

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

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

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

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

  App.mount(<div>
      <Header />
      <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>
      <Header />
      <ChangePasswordForm token={token} onSuccess={App.go.bind(App, '/signin')}/>
    </div>);
});

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

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

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

App.page('/not_confirmed', true, function(context) {
  App.mount(<div>
      <Header displayLogout={true}/>
      <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>
        <Header displayLogout={true}/>
        <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>
      <Header displayLogout={true}/>
      <OtpEnrollForm onSuccess={App.onUserValidateOtp} />
    </div>);
});

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

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