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

import ClassDataProxy from "proxy/ClassDataProxy";

import localConfig from "local/localConfig";
import UIMagnifyApe from "ape/UIMagnifyApe";
import UIControlApe from "ape/UIControlApe";
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 PartTimeTeachingApe from 'ape/PartTimeTeachingApe';//兼课
import ManagementHomePageApe from 'ape/ManagementHomePageApe';//管理员首页
import CreateClassApe from 'ape/CreateClassApe';//创建课堂
import CreateUserApe from 'ape/CreateUserApe';//创建用户界面
import CreateGradeApe from 'ape/CreateGradeApe';//创建班级界面
import AboutClassApe from 'ape/AboutClassApe';//约课界面
import ListDetailsApe from 'ape/ListDetailsApe';//列表详情
import AdministratorApe from 'ape/AdministratorApe';//管理员
import PlayRecordApe from 'ape/PlayRecordApe';//录制管理
import MyCenterApe from 'ape/MyCenterApe';//个人中心
import SubscribeApe from 'ape/SubscribeApe';//预约
import MessageTypes from "MessageTypes";


import Loger from "Loger";
let loger = Loger.getLoger('networkSchool');


let _loginOrRegister;
let _lanclassApe;
let _liveClassApe;
let _homeApe;
let _teacherDetailApe;
let _partTimeTeachingApe;
let _managementHomePageApe;
let _createClassApe;
let _createUserApe;
let _createGradeApe;
let _aboutClassApe;
let _listDetailsApe;
let _administratorApe;
let _playRecordApe;
let _myCenterApe;
let _subscribeApe;

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

        let locationProtocol= location.protocol;
        if(locationProtocol=="https:"){
            ClassDataProxy.locationProtocol="https://";
        }else {
            ClassDataProxy.locationProtocol="http://";
        }
        //获取国际化语言
        this._localConfig();
        //获取地址栏参数
        this.getURLParams();
        this.localStorage = window.localStorage;
        //判断当前用户登录身份状态
        this._localStorageState();
        //课堂初始化和启动流程
        this.init();
        //默认显示管理员首页
        this.switchClass(0);
    }

    //获取地址栏参数
    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 || "";
            ClassDataProxy.siteId = ClassDataProxy.userName;
        }
    }

    //将地址转化成一个对象
    _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();
        //兼课
        _partTimeTeachingApe = new PartTimeTeachingApe();
        //创建课堂
        _managementHomePageApe = new ManagementHomePageApe();
        //创建课堂
        _createClassApe = new CreateClassApe();
        _createClassApe.on(MessageTypes.ADD_CLASS,this.addClass.bind(this));
        //创建用户老师
        _createUserApe = new CreateUserApe();
        //创建班级
        _createGradeApe = new CreateGradeApe();
        //创建约课
        _aboutClassApe = new AboutClassApe();
        //列表详情
        _listDetailsApe = new ListDetailsApe();
        //管理员
        _administratorApe = new AdministratorApe();
        _administratorApe.on(MessageTypes.SWITCH_CLASS,this.switchClass.bind(this));
        //录制回放
        _playRecordApe = new PlayRecordApe();

        //个人中心
        _myCenterApe = new MyCenterApe();
        //预约管理
        _subscribeApe = new SubscribeApe();

    }


    //
    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();
                _managementHomePageApe.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();
            }
        }
    }
    switchClass(_data){
        let ind = parseInt(_data);

            loger.log('管理员列表',ind)
            switch (ind){
                case 0:
                    //首页
                    _managementHomePageApe.detailPage();
                    break;
                case 1:
                    //创建课堂
                    _createClassApe._createClassBtnHandler();
                    break;
                case 2:
                    //班级管理
                    _createGradeApe.userAdminHandler(1);
                    break;
                case 3:
                    //用户管理
                    _createUserApe.userAdminHandler(1);
                    break;
                case 4:
                    //约课管理
                    _aboutClassApe.userAdminHandler(1);
                    break;
                case 5:
                    //录制管理
                    _playRecordApe.detailPage(1);
                    break;
                case 6:
                    //站点配置
                    break;
                case 7:
                    //统计报告
                    break;
                case 8:
                    //个人设置
                    break;
            }
    }
    addClass(_data){
        loger.log('创建课堂成功')
        _managementHomePageApe.detailPage();
    }
    //判断登录用户的状态
    _localStorageState(){
        let userType = window.localStorage.getItem('userType')||0;
        let loginName = window.localStorage.getItem('loginName')|| '';
        let id = window.localStorage.getItem('id')|| '';
        let siteId = window.localStorage.getItem('siteId')|| 'markettest';

        let userMobile = window.localStorage.getItem('userMobile')|| '';
        let userEmail = window.localStorage.getItem('userEmail')|| '';
        let monicker = window.localStorage.getItem('monicker')|| '';
        let status = window.localStorage.getItem('status')|| '';

        ClassDataProxy.userType = parseInt(userType);
        ClassDataProxy.loginName = loginName;
        ClassDataProxy.id = id;
        ClassDataProxy.siteId = siteId;
        //个人信息
        ClassDataProxy.userMobile = userMobile;
        ClassDataProxy.userEmail = userEmail;
        ClassDataProxy.monicker = monicker;
        ClassDataProxy.status = status;
        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('兼课')
                this.partTimeTeaching();
                break;
            default:
                loger.log('游客')
                this._student();
                break;
        }
    }
    _showAdministratorInfo(){
        $('#slideshow').hide();
        $('#defaultBox').hide();
        $('.curriculumBox').hide();
        $('#header_list').hide();
        $('#header_seek').hide();
        $('#androidDownload').hide();
        $('#classManagementBox').show();
        $('#teacherDetailBox').hide();
        $('#myCenterBox').hide();
        $('#lanclassBox').hide();
        $('#classifyListBox').hide();
        $('#partTimeTeachingBox').hide();
        $('#recordUl .recordLi').eq(0).addClass('recordLiCheck').siblings().removeClass('recordLiCheck');
        $('.classManagementRight').eq(0).show().siblings().hide();
    }
    _teacherHome(){
        $('#slideshow').hide();
        $('#defaultBox').hide();
        $('.curriculumBox').hide();
        $('#header_list').hide();
        $('#header_seek').hide();
        $('#androidDownload').hide();
        $('#classManagementBox').hide();
        $('#classifyListBox').hide();
        $('#partTimeTeachingBox').hide();
        $('#myCenterBox').show();
        $('.myCenterContentBox').eq(0).show().siblings().hide();
        $('#teacherMyCerter').show();
        $('#studentMyCerter').hide();
        $('.recordLi2').eq(0).addClass('recordLiCheck').siblings().removeClass('recordLiCheck');
    }
    _student(){
        $('#slideshow').show();
        $('#defaultBox').show();
        $('.curriculumBox').show();
        $('#classifyListBox').hide();
        $('#teacherDetailBox').hide();
        $('#lanclassBox').hide();
        $('#myCenterBox').hide();
        $('#partTimeTeachingBox').hide();
        $('.myCenterContentBox').eq(0).show().siblings().hide();
        $('#teacherMyCerter').hide();
        $('#studentMyCerter').show();
        $('.recordLi2').eq(0).addClass('recordLiCheck').siblings().removeClass('recordLiCheck');
    }
    partTimeTeaching(){
        $('#slideshow').hide();
        $('#defaultBox').hide();
        $('.curriculumBox').hide();
        $('#header_list').hide();
        $('#header_seek').hide();
        $('#androidDownload').hide();
        $('#classManagementBox').hide();
        $('#classifyListBox').hide();
        $('#myCenterBox').hide();
        $('#partTimeTeachingBox').show();
    }
    //获取国际化语言
    _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();