﻿
var privacyLevels = new Array();
privacyLevels.push("");
privacyLevels.push("Public");
privacyLevels.push("Friends");
privacyLevels.push("Private");

function CheckForMirGame(response)
{
    var hasMir3Account = false;

    for (var i = 0; i < response.Games.length; i++)
    {
        if (response.Games[i] == "Mir3")
        {
            hasMir3Account = true;
            break;
        }
    }

    if (hasMir3Account)
    {
        Mir3Services.GetPlayerCharacters(null, EndGetMir3Characters);
    }
    else
    {
        ShowElement("mir3ClaimForm");
    }
}

function EndGetMir3Characters(response)
{
    if (!HandleCommonResults(response))
    {
        if (IsSuccessResult(response))
        {
            BuildCharacterList(response.Characters);
        }
        else
        {
            UnknownResult(response);
        }
    }
}

function BuildCharacterList(characters)
{
    var mir3List = document.getElementById("mir3List");
    mir3List.innerHTML = '<hr /><div class="mir3Header">My Characters</div><br />';
    for (var i = 0; i < characters.length; i++)
    {

        // char, lvl, pct
        var character = characters[i];
        
        var listItem = document.createElement("div");
        SetClass(listItem, "mir3ListItem");

        var classIcon = document.createElement("img");
        SetClass(classIcon, "mir3ClassIcon");
        classIcon.src = "gameProfiles/mir3/images/classicon_" + character.ClassName + ".png";
        classIcon.alt = character.ClassName;
        classIcon.title = character.ClassName;


        var expPct = Math.round(character.ExperiencePercentage);

        var characterLink = document.createElement("a");
        SetClass(characterLink, "mir3CharacterLink");
        characterLink.innerHTML = character.CharacterName + ", level " + character.CharacterLevel;

        if (expPct > 0)
        {
            characterLink.innerHTML += "." + expPct;
        }

        characterLink.title = character.CharacterName + ", level " + character.CharacterLevel + " " + character.GenderName + " " + character.ClassName;
        characterLink.title += "\r\n" + expPct + "% experience toward level " + (character.CharacterLevel + 1);
        characterLink.href = "gameProfiles/mir3?search=" + character.CharacterName;



        // privacy
        var privacyIcon = document.createElement("img");
        var cV = character.Visibility;
        if (cV == "Public")
        {
            privacyIcon.privacyLevel = 1;
        } else if (cV == "Friends")
        {
            privacyIcon.privacyLevel = 2;
        } else if (cV == "Private")
        {
            privacyIcon.privacyLevel = 3;
        } else
        {
            Error("Unknown character visibility state (" + cV + ") for character: " + character.CharacterName);
        }
        var nxtPLevel = privacyIcon.privacyLevel + 1;
        if (nxtPLevel > 3)
        {
            nxtPLevel = 1;
        }

        SetClass(privacyIcon, "mir3PrivacyIcon");
        privacyIcon.characterName = character.CharacterName;
        privacyIcon.src = "images/themes/default/privacy-icon-" + privacyLevels[privacyIcon.privacyLevel] + ".png";
        privacyIcon.alt = privacyLevels[privacyIcon.privacyLevel] + " privacy level set for " + privacyIcon.characterName + " - click to change to " + privacyLevels[nxtPLevel].toLowerCase() + " privacy level";
        privacyIcon.title = privacyLevels[privacyIcon.privacyLevel] + " privacy level set for " + privacyIcon.characterName + " - click to change to '" + privacyLevels[nxtPLevel].toLowerCase() + "' privacy level";
        privacyIcon.onclick = function(e)
        {
            if (e == null)
            {
                e = window.event;
            }
            var pIcon = GetEventSource(e);

            pIcon.privacyLevel++;
            if (pIcon.privacyLevel > 3)
            {
                pIcon.privacyLevel = 1;
            }
            var nextPLevel = pIcon.privacyLevel + 1;
            if (nextPLevel > 3)
            {
                nextPLevel = 1;
            }

            pIcon.src = "images/themes/default/privacy-icon-" + privacyLevels[pIcon.privacyLevel] + ".png";
            pIcon.alt = privacyLevels[pIcon.privacyLevel] + " privacy level set for " + pIcon.characterName + " - click to change to " + privacyLevels[nextPLevel].toLowerCase() + " privacy level";
            pIcon.title = privacyLevels[pIcon.privacyLevel] + " privacy level set for " + pIcon.characterName + " - click to change to " + privacyLevels[nextPLevel].toLowerCase() + " privacy level";

            //gameName, characterName, visibility, callback, state
            Mir3Services.SetCharacterVisibility("Mir3", pIcon.characterName, privacyLevels[pIcon.privacyLevel], function(response)
            {
                if (!HandleCommonResults(response))
                {
                    if (IsSuccessResult(response))
                    {
                    }
                    else
                    {
                        UnknownResult(response);
                    }
                }
            });

        };

        listItem.appendChild(privacyIcon);
        listItem.appendChild(classIcon);
        listItem.appendChild(characterLink);
        mir3List.appendChild(listItem);
        
        var clear = document.createElement("div");
        clear.style.clear = "both";
        mir3List.appendChild(clear);
    }

    var privacyKey = document.createElement("img");
    privacyKey.src = "images/themes/default/games-mir3-character-key.png";
    privacyKey.alt = "Character visibility key: privacy levels";
    privacyKey.title = "Character visibility key: privacy levels";
    privacyKey.style.width = "180px";
    privacyKey.style.height = "50px";
    privacyKey.style.marginLeft = "6px";

    mir3List.appendChild(privacyKey);    
}

function ClaimMir3AccountInputKeyPress(e)
{
    if (e == null)
    {
        e = window.event;
    }

    var keyCode = e.keyCode;

    if (keyCode == null)
    {
        keyCode = e.which;
    }

    if (keyCode == 13)//enter key
    {
        ClaimMir3Account();
    }
}

function ClaimMir3Account()
{
    var username = document.getElementById("mir3ClaimUsername").value;
    var pwd = document.getElementById("mir3ClaimPassword").value;

    if (username == null || username.length < 1)
    {
        alert("username can not be blank");
        return;
    }

    if (pwd == null || pwd.length < 1)
    {
        alert("password can not be blank");
        return;
    }

    document.getElementById("mir3ClaimPassword").value = "";

    HideElement("mir3ClaimForm");
    ShowElement("mir3ClaimWait");

    Mir3Services.ClaimAccount(username, pwd, EndClaimMir3Account);
}

function EndClaimMir3Account(response)
{
    HideElement("mir3ClaimWait");

    if (!HandleCommonResults(response))
    {
        if (IsSuccessResult(response))
        {
            alert("Your Mir III account has been successfully claimed!");

            location.reload();
            //Mir3Services.GetPlayerCharacters(EndGetMir3Characters);
        }
        else
        {
            ShowElement("mir3ClaimForm");
            document.getElementById("mir3ClaimUsername").select();

            if (response.Result == "InvalidCredentials")
            {
                alert("Invalid Mir III username or password.");
            }
            else if (response.Result == "GameAccountAlreadyExists")
            {
                alert("This Mir III account has already been claimed. If you believe this account has been claimed by the wrong person please contact customer support.");
            }
            else
            {
                UnknownResult(response);
            }
        }
    }
}
