/**
 * Created by adri1 on 2017/7/19.
 */
function inspectorShow(){
    if(!checkNowPage("tankinspector",true)){
        return;
    }
    if ($("#inspectorpage").html().length>0){
        $("#inspectorpage").html("");
    }

    //检测程序是否已经存在指定位置
    var exist=fs.existsSync("tankinspector\\Smellyriver.TankInspector.exe");

    $("#inspectorpage").load("tankinspector.html #tankinspector",function(data){
        if(checkNowPage("tankinspector",false)){
            return;
        }
        $("#inspectordownload").hide();
        $("#inspectorupdate").hide();
        if(!exist){
            $("#inspectorstart").hide();
            $("#inspectorclear").hide();
            $("#inspectordelete").hide();
        }else{
            $("#inspectorstart").click(function(){
                customAlert("装甲密档已启动，请稍等片刻","success");
                //启动游戏
                cp.exec("Smellyriver.TankInspector.exe",{
                    cwd: "tankinspector"
                });
            });
            $("#inspectorclear").click(function(){
                //删除缓存
                deleteFolderRecursive(process.env.APPDATA+"\\..\\Local\\Smellyriver",0);
                customAlert("装甲密档缓存清除完成","success");
                $(this).addClass("disabled");
            });
            $("#inspectordelete").click(function(){
                //删除文件
                customConfirm("确认要卸载装甲密档?",function(){
                    customAlert("装甲密档卸载完成","success");
                    deleteFolderRecursive("tankinspector",0);
                    $("#inspectordelete").addClass("disabled");
                });
            });
        }
        checkInspectorUpdate();
    });
}
function checkInspectorUpdate(){
    $.get("http://rank.kongzhong.com/ModDownload/action/PatchAction/getpatch?type=tankinspector", function(data, status) {
        try {
            var versionlist = JSON.parse(data);
            for (var ver in versionlist.patchlist) {
                var patch = versionlist.patchlist[ver];
                if (patch.url.indexOf("http") == -1) {
                    continue;
                }
                var url=patch.url;
                var name=patch.name;
                var version=patch.version;
                var local=versionDB.get(patch.name);
                if(!checkERROR(local)&&local<patch.version){
                    $("#inspectorupdate").click(function() {
                        customAlert(name+'开始安装','success');
                        updatePatch(url, name, version);
                    });
                    $("#inspectorupdate").show();
                    $("#inspectorupdate").attr('title',patch.description);
                    var content = "<div><small>("+version+"版)</small></div>";
                    $("#inspectorupdate").after(content);
                }
                if(!fs.existsSync("tankinspector\\Smellyriver.TankInspector.exe")){
                    $("#inspectordownload").click(function() {
                        customAlert(name+'开始安装','success');
                        updatePatch(url, name, version);
                    });
                    $("#inspectordownload").show();
                }
                break;
            }
        }catch(e){
            customAlert("加载patchlist失败","warning")
        }
    });
}