import React from 'react';
import Modularity from 'modularity-front';

import { Input, Button } from 'react-materialize';


import API from './API';

class UserService extends Modularity.Module{
    constructor(props){
        super(props);
        this.state = {
            username: '',
            password: ''
        }
        this.API = new API('/users', UserService.Manager.getToken());
        this.getUser(UserService.Manager.getToken());
    }

    getToken() {
        return UserService.Manager.getToken();
    }

    isLogged() {
        let token = UserService.Manager.getToken();
        return !!token && token !== "undefined" && token !== 'missing';
    }

    logout() {
        return this.API.get('/logout').then(() => {
            this.User.set({});
            this.Token.set('missing');
            UserService.Manager.removeToken();
        })
    }

    login(inputdata = this.state) {
        let {
            username,
            password
        } = inputdata;
        return this.API.post('/login', {
            username,
            password
        }).then((data) => this.getUser(data.token));
    }

    register(inputdata = this.state) {
        let {
            email,
            username,
            password
        } = inputdata;
        return this.API.post('/register', {
            email,
            username,
            password
        }).then((data) => this.getUser(data.token));
    }
    
    getUser(token) {
        this.API.setToken(token);
        return this.API.get('/me').then((data) => {
            this.User.set(data);
            this.Token.set(data.token || 'missing');
            UserService.Manager.setToken(data.token || 'missing');
            return data;
        }).catch(() => {
            this.User.set({});
            this.Token.set('missing');
            UserService.Manager.removeToken();
        });
    }

    confirmUser(token) {
        return this.API.get("/verifyemail?id=" + token);
    }

    changePass(token, cpass, npass) {
        this.API.setToken(token);
        return this.API.post('/changepass', {cpass, npass}).then((data) => {
            return data;
        })
    }
    render(){
        return(
            <div className="UserServiceInfo">
                ID : {this.__renderID()}
                <div>
                    <Input type="email" label={'Email'} value={this.state.email} onChange={(e) => this.setState({email: e.target.value})}/>
                    <Input type="text" label={'Username'} value={this.state.username} onChange={(e) => this.setState({username: e.target.value})}/>
                    <Input type="password" label={'Password'} value={this.state.password} onChange={(e) => this.setState({password: e.target.value})}/>
                    <Button onClick={() => this.login()}>Login</Button>
                    <Button onClick={() => this.register()}>Register</Button>
                </div>
                <div>
                    Token : {this.Token.value}
                </div>
                <div>
                    {this.__factoryOutput(this.User)}
                    {this.__factoryOutput(this.Token)}
                </div>
                <div>
                    {super.render()}
                </div>
            </div>
        )
    }
}


class Manager {
    constructor() {
        this.token = localStorage.getItem('Token');
    }

    setToken(token) {
        this.token = token;
        localStorage.setItem('Token', token);
    }

    getToken() {
        return this.token;
    }

    removeToken() {
        this.token = 'missing';
        localStorage.setItem('Token', 'missing');
    }
}

UserService.Inputs = {
}

UserService.Outputs = {
    User:{},
    Token: {}
}

UserService.Category = "Services";
UserService.Manager = new Manager();
export default UserService;