OneUi.use(["site", "addon", "user"], function (site, addon, user) {

    site.setMenu("one_pan_mine");

    addon.getInstall(function () {
        return window.OneContainer;
    }, function (OneContainer) {
        OneContainer.use(["runner"], function (runner) {
            runner.runScope("wpzs");
        });
    });

    addon.getInstall(function () {
        return window.OnePan;
    }, function (container) {
        container.use(["shareLog"], function (shareLog) {

            var matchQueryString = function (name, defaulValue) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var match = location.search.match(reg);
                if (match != null) {
                    return unescape(match[2]);
                }
                else {
                    return defaulValue;
                }
            };

            var config = {
                param: {
                    source: matchQueryString("source", "all"),
                    page: matchQueryString("page", 1)
                },
                page: {
                    total_count: 0,
                    total_page: 1,
                    page_id: 1,
                    page_size: 2
                }
            };

            new Vue({
                mixins: [site.getMixin()],
                data: {
                    install: true,
                    user: user.getStore(),
                    pwd_list: [],
                    param: config.param,
                    page: config.page
                },
                created: function () {
                    this.showPwdList(this.param.source);
                },
                methods: {
                    showPwdList: function (source) {
                        this.param.source = source;

                        this.loadPwdList();
                    },
                    deletePwd: function (item) {
                        shareLog.removeShareLog(item.share_source, item.share_id, this.loadPwdList);
                    },
                    loadPwdList: function () {
                        var that = this;

                        history.replaceState(null, null, that.httpBuildQuery(location.pathname, this.param));

                        shareLog.getShareLogList(function (shareLogList) {
                            that.pwd_list = that.processPwdList(shareLogList, that.param.source);
                        });
                    },
                    processPwdList: function (shareLogList, shareSource) {
                        var filterShareLogList = [];
                        for (var i in shareLogList) {
                            var item = shareLogList[i];
                            if (shareSource === "all" || shareSource === item.share_source) {
                                filterShareLogList.push({
                                    share_source: item.share_source,
                                    share_id: item.share_id,
                                    share_pwd: item.share_pwd,
                                    share_link: this.buildShareLink(item.share_source, item.share_id, item.share_link),
                                    share_time: shareLog.buildShareTime(item.share_time),
                                    create_time: item.share_time
                                });
                            }
                        }

                        filterShareLogList = filterShareLogList.sort(function (a, b) {
                            return b.create_time - a.create_time;
                        });

                        this.page.total_count = filterShareLogList.length;
                        this.page.total_page = Math.ceil(this.page.total_count / this.page.page_size);

                        var offset = (this.param.page - 1) * this.page.page_size;
                        return filterShareLogList.slice(offset, offset + this.page.page_size);
                    },
                    buildShareLink: function (shareSource, shareId, shareLink) {
                        if (shareSource === "baidu") {
                            shareLink = "https://pan.baidu.com/s/1" + shareId;
                        } else if (shareSource === "weiyun") {
                            shareLink = "https://share.weiyun.com/" + shareId;
                        } else if (shareSource === "lanzous") {
                            shareLink = "https://www.lanzous.com/" + shareId;
                        } else if (shareSource === "ty189") {
                            shareLink = "https://cloud.189.cn/t/" + shareId;
                        }
                        return shareLink;
                    },
                    goPage: function (page) {
                        this.param.page = page;

                        this.loadPwdList();
                    },
                    goFirstPage: function () {
                        this.goPage(1);
                    },
                    goPrevPage: function () {
                        if (this.param.page > 2) {
                            this.goPage(parseInt(this.param.page) - 1);
                        } else {
                            this.goFirstPage();
                        }
                    },
                    goTargetPage: function () {
                        var page = parseInt(this.param.page);
                        if (page < 1) {
                            page = 1;
                        } else if (page > this.page.total_page) {
                            page = this.page.total_page;
                        }
                        this.goPage(page);
                    },
                    goNextPage: function () {
                        if (this.param.page < this.page.total_page) {
                            this.goPage(parseInt(this.param.page) + 1);
                        } else {
                            this.goEndPage();
                        }
                    },
                    goEndPage: function () {
                        this.goPage(this.page.total_page);
                    },
                    httpBuildQuery: function (url, param) {
                        for (var i in param) {
                            if (url.indexOf("?") < 0) {
                                url += "?";
                            } else {
                                url += "&";
                            }
                            url += i + "=" + param[i];
                        }
                        return url;
                    }
                }
            });
        });
    });

});