#include "${LavishScript.HomeDirectory}/scripts/mr/common/JsonHelpers.iss"

objectdef SettingsController 
{

    variable jsonvalue abilityListItems = "[]"
    variable jsonvalue profile = "{}"
    variable jsonvalue currentCastStack = "[]"
    variable int currentSelectedAvailableAbilityIndex = -1
    variable jsonvalue newCastStackItem = "{}"

    method Initialize()
    {
        LavishScript:RegisterEvent[onNewCastStackItemChanged]
        variable jsonvalue jo
        
        jo:ParseFile["${LavishScript.HomeDirectory}/scripts/mr/bot/ability_exports/${Me.Name}_${Me.SubClass}.json"]
        if ${jo.Type.NotEqual["object"]}
        {
            echo "Error parsing ability list"
            return
        }

        variable jsonvalueref keys
        keys:SetReference[jo.Keys]
        variable int idx
        for (idx:Set[1] ;  ${idx} < ${keys.Used} ; idx:Inc)
        {
            variable string key
            key:Set["${keys.Get[${idx}]}"]
            variable jsonvalue abilityItem = "{}"
            if ${jo.Get["${key}"].Get[1, "spellBookType"]} < 2
            {
                abilityItem:SetValue["$$>{
                    "name": "${key~}",
                    "targertType": ${jo.Get["${key}"].Get[1, "targetType"]},
                    "isBeneficial": ${jo.Get["${key}"].Get[1, "isBeneficial"]},
                    "groupRestricted": ${jo.Get["${key}"].Get[1, "groupRestricted"]},
                    "maxAoeTargets": ${jo.Get["${key}"].Get[1, "maxAoeTargets"]}

                }<$$"]
                abilityListItems:Add["${abilityItem.AsJSON}"]
            }
        }

        jo:ParseFile["${LavishScript.HomeDirectory}/scripts/mr/bot/profiles/ihaihu_shadowknight.json"]
        if ${jo.Type.NotEqual["object"]}
        {
            echo "Error parsing profile"
            return
        }
        
        profile:SetValue["${jo.Get[${jo.Get["default"]}].AsJSON}"]

    }

    method Shutdown()
	{
	}
    
    method OnClose()
    {
    }

    method ToggleTargetVisibilityBySelectedAbility(int selectedAbilityIndex)
    {
        if ${abilityListItems.Get[${selectedAbilityIndex}, "TargetType"]} != 1
        {
            LGUI2.Element["castStack.ability.target"].Parent:SetVisibility[Hidden]
        }
        else
        {
            LGUI2.Element["castStack.ability.target"].Parent:SetVisibility[Visible]
        }
    }

    method ToggleNumTargetsBySelectedAbilityId(int selectedAbilityIndex)
    {
        if ${abilityListItems.Get[${selectedAbilityIndex}, "MaxAoeTargets"]} > 1
        {
            LGUI2.Element["castStack.ability.targetCount"].Parent:SetVisibility[Visible]
        }
        else
        {
            LGUI2.Element["castStack.ability.targetCount"].Parent:SetVisibility[Hidden]
        }
    }

    method SetCurrentAbility(int selectedIndex)
    {

        This:ToggleTargetVisibilityBySelectedAbility[${selectedIndex}]
        This:ToggleNumTargetsBySelectedAbilityId[${selectedIndex}]
        
        currentSelectedAvailableAbilityIndex:Set[${selectedIndex}]
        LGUI2.Element[MRSettingsController.events]:FireEventHandler[onNewCastStackItemChanged]
        newCastStackItem:SetValue["$$>{
            "action": "ability",
            "name": "${abilityListItems.Get[${selectedIndex}, "name"]}"
        }<$$"]
    }

    method OnCastStackAbilityComboChange(string param)
    {
        This:SafeSetNewCastStackItemProperty["${Context.Source.Name.Token[3, "."]}", "${Context.Source.SelectedItem.Data.AsJSON~}"]
    }

    method SafeSetNewCastStackItemProperty(string propertyName, string value)
    {
        if ${value(exists)} && ${value.NotEqual[""]}
        {
            newCastStackItem:Set["${propertyName}", "${value}"]
        }
    }

    member:string SafeGetNewCastStackItemProperty(string propertyName)
    {
        if ${newCastStackItem.Has[${propertyName}]}
        {
            return ${newCastStackItem.Get[${propertyName}]}
            
        }

        return ""
    }
    member GetTargetOptions()
    {
        variable jsonvalue targetOptions

        targetOptions:SetValue[$$>"[
            "@Group",
            "@Raid",
            "@Me",
            "@Tank",
            "@Healer",
            "@DPS"
        ]"<$$]

        ; TODO: Add alias options once aliases are supported
        return ${targetOptions}
    }

    method AddAbility()
    {
        currentCastStack:Add["${newCastStackItem.AsJSON}"]
    }
}

