import React,{Component} from 'react'


import Login from '../views/Login'
import Layout from '../views/Layout'
import { inject, observer } from 'mobx-react'
import {LocaleProvider} from 'antd';
import {IntlProvider, addLocaleData} from 'react-intl'; //国际化依赖
import en from 'react-intl/locale-data/en';
import zh from 'react-intl/locale-data/zh';


import S from '../utils/Storage'
import { HashRouter as Router,Switch, Route, Redirect } from 'react-router-dom';

import {zhCN,enUS} from '../config/i18n'

import antd_zh_CN from 'antd/lib/locale-provider/zh_CN';
import antd_en_US from 'antd/lib/locale-provider/en_US';
import Lang from './Lang'
import Display from '../views/Display'
import NoLang from './NoLang'

addLocaleData([...en,...zh])

@inject('i18n')
@inject('UserContext')
@observer
export default class AppRouter extends Component
{
    constructor(props)
    {
        super(props)
    }
    componentWillMount()
    {
        
    }
    reloadFromeStorage()
    {
        //负责程序登录验证
        //信息重加载
        let userId = S.getUser()
        let token = S.getToken()
        if(userId && token )
        {
            this.props.UserContext.userId = userId
            this.props.UserContext.token  = token
            this.props.UserContext.menu = S.getMenu()||[]
        }
    }

    renderCoreRouter(lang)
    {
        return    <Switch>
                                <Route exact  path='/' render={props =>this.props.UserContext.token? <Redirect to={`/business/home`}  />:null}/>
                                <Route
                                    exact
                                    path={`/${lang}/login`}
                                    render={props => <Login {...props} />}
                                />
                                <Route path={`/${lang}/business`} render={props => <Layout {...props} />} />
                                <Redirect  path={`/${lang}/home`} to={`/${lang}/business/home`} />}/>
                                <Route path={`/${lang}/display/:trnnam/:trninr`} render={props => <Display {...props} />} />
                </Switch>
    }

    render()
    {   
        return (
            <IntlProvider locale={this.props.i18n.lang||'en'} messages={this.props.i18n.lang=='cn'?zhCN:enUS}>
                <LocaleProvider locale={this.props.i18n.lang=='en'?antd_en_US:antd_zh_CN}>
                        <Router>

                            <Switch>
                            <Route path="/en" render={props=> <Lang lang="en">{this.renderCoreRouter('en')}</Lang> } />
                            <Route path="/cn" render={props=> <Lang lang="cn">{this.renderCoreRouter('cn')}</Lang> } />
                            <Route path="/" render={props=><NoLang>
    
                                    <Route exact  path='/' render={props => <Redirect to={this.props.UserContext.token?`/business/home`:'/login'}  />}/>
                                    <Route  path='/login' render={props => <Login {...props} />}/>
                                    
                                    <Route path='/business' render={props => <Layout {...props} />} />
                                    <Route path='/display/:trnnam/:trninr' render={props => <Display {...props} />} />
                                </NoLang>}
                            />
                            </Switch>
                        </Router>
                </LocaleProvider>
            </IntlProvider>
        )
    }
}