import $ from 'jquery';
import url from 'url';

import ClassDataProxy from "proxy/ClassDataProxy";

import localConfig from "local/localConfig";
import UIMagnifyApe from "ape/UIMagnifyApe";
import ErrorApe from 'ape/ErrorApe';


import Slideshow from 'ape/Slideshow';//图片轮播
import LoginOrRegister from 'ape/LoginOrRegister';//登录注册
import LanclassApe from 'ape/LanclassApe';//互动课堂
import LiveClassApe from 'ape/LiveClassApe';//直播课堂
import HomeApe from 'ape/HomeApe';//首页
import TeacherDetailApe from 'ape/TeacherDetailApe';//老师
import CreateClassApe from 'ape/CreateClassApe';//创建课堂
import ListDetailsApe from 'ape/ListDetailsApe';//列表详情
import AdministratorApe from 'ape/AdministratorApe';//管理员
import MyCenterApe from 'ape/MyCenterApe';//个人中心
import MessageTypes from "MessageTypes";
import Loger from "Loger";
let loger = Loger.getLoger('networkSchool');


let _loginOrRegister;
let _lanclassApe;
let _liveClassApe;
let _homeApe;
let _teacherDetailApe;
let _createClassApe;
let _listDetailsApe;
let _administratorApe;
let _myCenterApe;

class Main {
    constructor() {
        this.clientVersion="v1.5.4.20170903";
        loger.warn("clientVersion:"+this.clientVersion);

        let locationProtocol= location.protocol;
        if(locationProtocol=="https:"){
            ClassDataProxy.locationProtocol="https://";
        }else {
            ClassDataProxy.locationProtocol="http://";
        }
        this.localStorage = window.localStorage;
        //判断当前用户登录身份状态
        this._localStorageState();
        //获取国际化语言
        this._localConfig();
        //获取地址栏参数
        this.getURLParams();

        //课堂初始化和启动流程
        this.init();
    }

    //获取地址栏参数
    getURLParams() {
        let urlParams = this._parseUrlParams();
        loger.log("urlParams", urlParams);
        if (urlParams) {
            ClassDataProxy.classId = parseInt(urlParams.classId) || 0;

          let userName = urlParams.userName || "";
            ClassDataProxy.userName = decodeURI(userName);
            ClassDataProxy.userId = urlParams.userId || userName;

            ClassDataProxy.userRole = urlParams.userRole || "normal";
            ClassDataProxy.isRecordPlayBack = Boolean(urlParams.playRecord);
            ClassDataProxy.autoLogin = urlParams.m || 0;

          ClassDataProxy.maxMediaChannels=parseInt(urlParams.channels)||0;
          ClassDataProxy.userRole = urlParams.userRole;
          if(ClassDataProxy.userRole!=ClassDataProxy.USER_HOST&&ClassDataProxy.userRole!=ClassDataProxy.USER_INVISIBLE){
            ClassDataProxy.userRole=ClassDataProxy.USER_NOTMAL;
          }
            //IP+PORT
            let portal = urlParams.portalIP || "";
            let port = urlParams.portalPort || "";
            if (port) {
                portal = portal + ":" + port;
            }
            ClassDataProxy.portal = portal || "";
        }
    }

    //将地址转化成一个对象
    _parseUrlParams() {
        let url = encodeURI(location.search);
        let thisParam = {};
        let strs;
        // 判断是否存在请求的参数
        if (url.indexOf("?") != -1) {
            let str = url.substr(1);
            // 截取所有请求的参数,以数组方式保存
            strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                // 获取该参数名称,值。其中值以unescape()方法解码,有些参数会加密
                thisParam[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        // 返回改参数列表对象
        return thisParam;
    }

    //初始化界面
    init() {
        loger.log("init");

        //显示图片大图
        UIMagnifyApe.init($('#magnifyBox'));

        //登录注册
        _loginOrRegister = new LoginOrRegister();
        _loginOrRegister.on(MessageTypes.LOGIN_OUT,this.loginOut.bind(this));
        _loginOrRegister.on(MessageTypes.LOGIN_IN,this.loginIn.bind(this));

        //互动课堂
        _lanclassApe = new LanclassApe();
        //互动课堂
        _liveClassApe = new LiveClassApe();
        //首页
        _homeApe = new HomeApe();
        _homeApe.on(MessageTypes.HOME_SKIP,this.homeSkip.bind(this));
        //老师
        _teacherDetailApe = new TeacherDetailApe();
        //创建课堂
        _createClassApe = new CreateClassApe();
        //列表详情
        _listDetailsApe = new ListDetailsApe();
        //管理员
        _administratorApe = new AdministratorApe();

        //个人中心
        _myCenterApe = new MyCenterApe();

    }


    //
    loginOut(_data){
        //
        if(_homeApe){
            _homeApe.detailPage();
        }
    }
    loginIn(_data){
        loger.log('进入课堂时显示的type值---登录',_data.userType)
        if(_data && _data.userType){
            if(_data.userType == ClassDataProxy.USER_TYPE_1){
                this._teacherHome();
                _teacherDetailApe.detailPage();
            }else if(_data.userType == ClassDataProxy.USER_TYPE_2){
                this._showAdministratorInfo();
                _administratorApe.detailPage();
            }else if(_data.userType == ClassDataProxy.USER_TYPE_8){
                _homeApe.detailPage();
            }else if(_data.userType == ClassDataProxy.USER_TYPE_32){

            }else{

            }
        }
    }
    homeSkip(_data) {
        if (_data && _data.userType) {
            if (_data.userType == ClassDataProxy.USER_TYPE_1) {
                _teacherDetailApe.detailPage();
            }
        }
    }
    //判断登录用户的状态
    _localStorageState(){
        let userType = window.localStorage.getItem('userType')||0;
        ClassDataProxy.userType = parseInt(userType);
        loger.log('进入课堂时显示的type值',ClassDataProxy.userType)

        switch (parseInt(userType)){
            case ClassDataProxy.USER_TYPE_1:
                loger.log('老师')
                this._teacherHome();
                break;
            case ClassDataProxy.USER_TYPE_2:
                loger.log('管理员')
                this._showAdministratorInfo();
                break;
            case ClassDataProxy.USER_TYPE_8:
                loger.log('学生')
                this._student();
                break;
            case ClassDataProxy.USER_TYPE_32:
                loger.log('兼课')
                break;
            default:
                loger.log('游客')
                this._student();
                break;
        }
    }
    _showAdministratorInfo(){
        $('#slideshow').hide();
        $('#defaultBox').hide();
        $('.curriculumBox').hide();
        $('#header_list').hide();
        $('#header_seek').hide();
        $('#androidDownload').hide();
        $('#classManagementBox').show();
    }
    _teacherHome(){
        $('#slideshow').hide();
        $('#defaultBox').hide();
        $('.curriculumBox').hide();
        $('#header_list').hide();
        $('#header_seek').hide();
        $('#androidDownload').hide();
        $('#classManagementBox').hide();
        $('#classifyListBox').hide();
    }
    _student(){
        $('#slideshow').show();
        $('#defaultBox').show();
        $('.curriculumBox').show();
        $('#classifyListBox').hide();
        $('#teacherDetailBox').hide();
        $('#lanclassBox').hide();
    }
    //获取国际化语言
    _localConfig() {
        let language = navigator.language || navigator.browserLanguage;
        language = language.toLocaleLowerCase();
       // loger.log("language:", language);
        if (language == "en-us" || language == "en-gb") {
            language = "en-us";
            ErrorApe.language = 'en_us';
        } else {
            language = "zh-cn";
        }
       // loger.log("language-use:", language);
        ClassDataProxy.localConfig = localConfig[language];

        if (!ClassDataProxy.localConfig) {
            ClassDataProxy.localConfig = localConfig['zh-cn'];
        }
    }
}
module.exports = new Main();