var util = require("./util")
var config = require("./config");
var fs = require("fs");

var platform, gameId, pureGameId;
function updateXcodeProj() {

    platform = config.compileOptions.platform;
    pureGameId = config.compileOptions.pureGameId;
    gameId = config.compileOptions.gameId;

    //////////
    var xcodeProjFolder = config.publishFolderRoot(platform) + "/HelloCordova.xcodeproj";
    var newXcodeProjFolder = config.publishFolderRoot(platform) + "/" + pureGameId + ".xcodeproj";

    if(fs.existsSync(xcodeProjFolder)) {

        if(!fs.existsSync(newXcodeProjFolder)) {
            fs.mkdirSync(newXcodeProjFolder);
        }

        util.copyDir(xcodeProjFolder, newXcodeProjFolder);
        util.deleteFolderRecursive(xcodeProjFolder);

        replaceGameStr(newXcodeProjFolder +"/project.pbxproj");

        ///////////////////
        var xcodeProjFolder = config.publishFolderRoot(platform) + "/HelloCordova.xcworkspace";
        var newXcodeProjFolder = config.publishFolderRoot(platform) + "/" + pureGameId + ".xcworkspace";

        if(!fs.existsSync(newXcodeProjFolder)) {
            fs.mkdirSync(newXcodeProjFolder);
        }

        util.copyDir(xcodeProjFolder, newXcodeProjFolder);
        util.deleteFolderRecursive(xcodeProjFolder);

        replaceGameStr(newXcodeProjFolder + "/contents.xcworkspacedata");
        replaceGameStr(newXcodeProjFolder + "/xcshareddata/xcschemes/HelloCordova.xcscheme", newXcodeProjFolder + "/xcshareddata/xcschemes/" +  + ".xcscheme");
        replaceGameStr(newXcodeProjFolder + "/xcuserdata/long.xcuserdatad/xcschemes/xcschememanagement.plist");

        //////////////
        var xcodeProjFolder = config.publishFolderRoot(platform) + "/HelloCordova";
        var newXcodeProjFolder = config.publishFolderRoot(platform) + "/" + pureGameId ;

        if(!fs.existsSync(newXcodeProjFolder)) {
            fs.mkdirSync(newXcodeProjFolder);
        }

        util.copyDir(xcodeProjFolder, newXcodeProjFolder);
        util.deleteFolderRecursive(xcodeProjFolder);

        replaceGameStr(newXcodeProjFolder + "/config.xml");
        replaceGameStr(newXcodeProjFolder + "/HelloCordova-Info.plist", newXcodeProjFolder + "/" + pureGameId + "-Info.plist");
        fs.unlinkSync(newXcodeProjFolder + "/HelloCordova-Info.plist");

        replaceGameStr(newXcodeProjFolder + "/HelloCordova-Prefix.pch", newXcodeProjFolder + "/" + pureGameId + "-Prefix.pch");
        fs.unlinkSync(newXcodeProjFolder + "/HelloCordova-Prefix.pch");
    }

    //////////////
    var localeEn = config.publishFolderRoot(platform) + "/" + pureGameId + "/Resources/en.lproj/InfoPlist.strings";
    if(config.compileOptions.gameName_en && config.compileOptions.gameName_en.length) {
        var str = fs.readFileSync(localeEn, "utf8");
        str = str.replace("ETDOGS", config.compileOptions.gameName_en);
        fs.writeFileSync(localeEn, str);
    }
    var localeCn = config.publishFolderRoot(platform) + "/" + pureGameId + "/Resources/zh-Hans.lproj/InfoPlist.strings";
    if(config.compileOptions.gameName && config.compileOptions.gameName.length) {
        var str = fs.readFileSync(localeCn, "utf8");
        str = str.replace("ET狗", config.compileOptions.gameName);
        fs.writeFileSync(localeCn, str);
    }


}

function replaceGameStr(file, newFile) {

    var str = fs.readFileSync(file, "utf8");
    str = str.replace(/com\.longames\.treeHero/g, gameId);
    str = str.replace(/HelloCordova/g, pureGameId);

    fs.writeFileSync(newFile || file, str);
}

module.exports = updateXcodeProj;