define([
    'require',
    'api/home',
    'api/auth',
    'api/public',
    'api/special'
], function (require, homeApi, authApi, publicApi, specialApi) {
    return {
        provide: function () {
            return {
                getUserLogin: this.getUserLogin,
                getUserInfo: this.getUserInfo
            };
        },
        data: function () {
            return {
                userInfo: null,
                publicData: null,
                agreeContent: null,
                agreeVisible: false,
                isLogin: false,
                currentRoute: window.location.pathname
            };
        },
        router: this.routes,
        created: function () {
            this.getUserLogin();
            this.getUserInfo();
            this.getPublicData();
            this.getHostSearch();
            this.getHomeNavigation();
            this.getGradeCate();
            this.getAgreeContent();
            this.createWebsiteStatistics();
        },
        methods: {
            getUserLogin: function () {
                homeApi.user_login().then(function () {
                    this.isLogin = true;
                }.bind(this)).catch(function () {
                    this.isLogin = false;
                    if (window.location.pathname.indexOf('/web/my/index') != -1) {
                        window.location.replace(this.$router.home);
                    }
                }.bind(this));
            },
            getPublicData: function () {
                publicApi.public_data().then(function (res) {
                    this.setPublicData(res.data);
                    if (res.data.customer_service === '2' && res.data.kefu_token && res.data.service_url) {
                        this.createCustomerServer({
                            openUrl: res.data.service_url,
                            token: res.data.kefu_token
                        });
                    }
                }.bind(this));
            },
            getUserInfo: function () {
                authApi.user_info().then(function (res) {
                    this.userInfo = res.data;
                }.bind(this));
            },
            getHostSearch: function () {
                publicApi.get_host_search().then(function (res) {
                    this.setPublicData({host_search: res.data});
                }.bind(this));
            },
            getHomeNavigation: function () {
                publicApi.get_home_navigation().then(function (res) {
                    this.setPublicData({navList: res.data});
                }.bind(this));
            },
            getGradeCate: function () {
                specialApi.get_grade_cate().then(function (res) {
                    this.setPublicData({grade_cate: res.data});
                }.bind(this));
            },
            getAgreeContent: function () {
                publicApi.agree().then(function (res) {
                    this.agreeContent = res.data;
                }.bind(this));
            },
            setPublicData: function (obj) {
                this.publicData = Object.assign({}, this.publicData || {}, obj);
            },
            createCustomerServer: function (option) {
                var vm = this;
                if (typeof initCustomerServer == 'function') {
                    if (typeof useCustomerServer == 'object') {
                        useCustomerServer.destroy();
                    }
                    window.useCustomerServer = new initCustomerServer({
                        openUrl: option.openUrl,
                        token: option.token,
                        isShowTip: false,
                        sendUserData: option.sendUserData || {}
                    });
                    useCustomerServer.init();
                } else {
                    require(['//chat.crmeb.net/customerServer.js'], function () {
                        vm.createCustomerServer(option);
                    });
                }
            },
            createWebsiteStatistics: function () {
                var request = new XMLHttpRequest();
                request.onreadystatechange = function () {
                    if (request.readyState === request.DONE) {
                        if (request.status === 200) {
                            var text = request.responseText;
                            if (text) {
                                var markStart = text.indexOf('<script>');
                                if (markStart !== -1) {
                                    text = text.slice(markStart + 8);
                                    var markEnd = text.indexOf('</script>');
                                    if (markEnd !== -1) {
                                        text = text.slice(0, markEnd);
                                    }
                                }
                                var scriptEl = document.createElement('script');
                                scriptEl.innerHTML = text;
                                document.body.appendChild(scriptEl);
                            }
                        }
                    }
                };
                request.open('GET', '../public_api/get_website_statistics');
                request.send();
            }
        }
    }
});