/*
 This file is a part of libertysoil.org website
 Copyright (C) 2015  Loki Education (Social Enterprise)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import Helmet from 'react-helmet';

import { ArrayOfMessages as ArrayOfMessagesPropType } from '../prop-types/messages';
import { CurrentUser as CurrentUserPropType } from '../prop-types/users';

import ApiClient from '../api/client';
import { API_HOST } from '../config';
import { ActionsTrigger } from '../triggers';
import { defaultSelector } from '../selectors';

import {
  Page,
  PageMain,
  PageBody,
  PageContent
} from '../components/page';
import Footer from '../components/footer';
import Login from '../components/login';
import Register from '../components/register';
import Header from '../components/header';
import HeaderLogo from '../components/header-logo';
import Messages from '../components/messages';

export class Auth extends React.Component {

  static propTypes = {
    current_user: CurrentUserPropType,
    is_logged_in: PropTypes.bool,
    messages: ArrayOfMessagesPropType.isRequired,
    ui: PropTypes.shape({
      registrationSuccess: PropTypes.bool
    }).isRequired
  };

  static async fetchData(router, store) {
    const props = store.getState();
    const currentUserId = props.get('current_user').get('id');
    const isLoggedIn = (currentUserId !== null);

    if (!isLoggedIn) {
      return null;
    }

    const currentUser = props.get('users').get(currentUserId);
    const more = currentUser.get('more');
    const is_first_login = !more || more.get('first_login');

    if (is_first_login) {
      return { status: 307, redirectTo: '/induction' };
    }

    return { status: 307, redirectTo: '/' };
  }

  render() {
    const {
      current_user,
      is_logged_in,
      messages,
      ui
    } = this.props;

    const client = new ApiClient(API_HOST);
    const triggers = new ActionsTrigger(client, this.props.dispatch);

    let renderedMessages;

    if (messages.length) {
      renderedMessages = (
        <div className="page__messages">
          <div className="page__body page__body-small">
            <Messages messages={messages} removeMessage={triggers.removeMessage} />
          </div>
        </div>
      );
    }

    const registration_success = ui.registrationSuccess;

    return (
      <div className="font-open_sans font-light">
        <Helmet title="Login to " />
        <section className="landing landing-big landing-bg landing-bg_house">
          <Header
            is_logged_in={is_logged_in}
            current_user={current_user}
            className="header-transparent"
          >
            <HeaderLogo />
          </Header>

          <header className="landing__body">
            <p className="layout__row layout__row-small landing__small_title" style={{ position: 'relative', left: 4 }}>Welcome to LibertySoil.org</p>
            <h1 className="landing__subtitle landing__subtitle-narrow">Education change network</h1>
            <Login onLoginUser={triggers.login} />
          </header>
        </section>

        <Page className="page__container-no_spacing page__container-bg">
          <PageMain>
            <PageBody className="page__body-small">
              <PageContent className="page__content-mobile_space">
                {renderedMessages}
                <div className="layout__row">
                  <Register
                    registration_success={registration_success}
                    onShowRegisterForm={triggers.showRegisterForm}
                    onRegisterUser={triggers.registerUser}
                  />
                </div>
              </PageContent>
            </PageBody>
          </PageMain>
        </Page>

        <Footer />
      </div>
    );
  }
}

export default connect(defaultSelector)(Auth);

@@ -1157,23 +1157,43 @@
 mport %7B 
-default
+createSelector, currentUser
 Selector
@@ -2511,24 +2511,143 @@
 his.props;%0A%0A
+    const current_user_js = current_user.toJS(); // FIXME #662%0A    const messages_js = messages.toJS(); // FIXME #662%0A%0A
     const cl
@@ -2792,16 +2792,19 @@
 messages
+_js
 .length)
@@ -2970,16 +2970,19 @@
 messages
+_js
 %7D remove
@@ -3101,16 +3101,21 @@
 ss = ui.
+get('
 registra
@@ -3125,16 +3125,18 @@
 nSuccess
+')
 ;%0A%0A    r
@@ -3404,16 +3404,19 @@
 ent_user
+_js
 %7D%0A      
@@ -4594,16 +4594,222 @@
 %0A  %7D%0A%7D%0A%0A
+const selector = createSelector(%0A  currentUserSelector,%0A  state =%3E state.get('messages'),%0A  state =%3E state.get('ui'),%0A  (current_user, messages, ui) =%3E (%7B%0A    messages,%0A    ui,%0A    ...current_user%0A  %7D)%0A);%0A%0A
 export d
@@ -4827,16 +4827,9 @@
 ect(
-defaultS
+s
 elec

Create selector for Auth component