class AutoBSS extends UICommonAPI;
const LOOP_COUNT = 1;
var WindowHandle Me;
var ItemWindowHandle Shot3;
var ItemWindowHandle Shot4;
var ItemWindowHandle InvItem;
var AnimTextureHandle Animation3;
var AnimTextureHandle Animation4;
var TextureHandle texBG3;
var ItemInfo BeastSoulShot;
var ItemInfo BeastSpiritShot;
var int BeastsoulIndex;
var int BeastspiritIndex;
var bool isUsedBeastSoul;
var bool isUsedBeastSpirit;
var bool isExistBeastSoul;
var bool isExistBeastSpirit;
var bool isAttacking;
var SkillInfo UsedSkill;
function OnLoad()
{
    Me = GetWindowHandle("AutoBSS");
    Shot3 = GetItemWindowHandle("AutoBSS.Item3");
    Shot4 = GetItemWindowHandle("AutoBSS.Item4");
    texBG3 = GetTextureHandle("AutoBSS.BGBeastSpirit");
    Animation3 = GetAnimTextureHandle("AutoBSS.Anim3");
    Animation4 = GetAnimTextureHandle("AutoBSS.Anim4");
    InvItem = GetItemWindowHandle("InventoryWnd.InventoryItem");
}                 
function OnRegisterEvent()
{
       RegisterEvent(EV_GamingStateEnter);
    RegisterEvent(EV_UpdateUserInfo);
    RegisterEvent(EV_ReceiveMagicSkillUse);
    RegisterEvent(EV_ReceiveAttack);
    RegisterEvent(EV_PetStatusShow);
    RegisterEvent(EV_SummonedStatusShow);
    RegisterEvent(EV_PetSummonedStatusClose);
}
function OnEvent( int EventID, String param )
{
    switch(EventID)
    {
        
        case EV_PetSummonedStatusClose:
             Me.HideWindow();
        break;
        case EV_GamingStateEnter:
                SetBSSPosition();
        break;
        
        case EV_PetStatusShow:
                SetBSSPosition();
                EnterTheVoid2();
    
        break;
        case EV_SummonedStatusShow:
                SetBSSPosition();
                EnterTheVoid2();
                
        break;
    
        case EV_UpdateUserInfo:
            if (GetUIState() == "GAMINGSTATE"&&
                class'UIAPI_CHECKBOX'.static.IsChecked("FlexOptionWnd.enableAutoBSS"))
                //EnterTheVoid2();
        break;
        case EV_ReceiveMagicSkillUse:
            if (GetUIState() == "GAMINGSTATE"&&
                class'UIAPI_CHECKBOX'.static.IsChecked("FlexOptionWnd.enableAutoBSS"))
                BeastSkillCast(param);
        break;
        case EV_ReceiveAttack:
            if (GetUIState() == "GAMINGSTATE"&&
                class'UIAPI_CHECKBOX'.static.IsChecked("FlexOptionWnd.enableAutoBSS"))
                AttackBeast(param);
        break;
    }
}
function SetBSSPosition()
{
    local bool IsExpand1;
    local bool IsExpand2;
    local bool IsExpand3;
    local bool IsExpand4;
    local bool IsExpand5;
    
    local bool isVertical;
    
    isVertical = GetOptionBool( "Game", "IsShortcutWndVertical" );
    
    IsExpand1 = GetOptionBool( "Game", "Is1ExpandShortcutWnd" );
    IsExpand2 = GetOptionBool( "Game", "Is2ExpandShortcutWnd" );
    IsExpand3 = GetOptionBool( "Game", "Is3ExpandShortcutWnd" );
    IsExpand4 = GetOptionBool( "Game", "Is4ExpandShortcutWnd" );
    IsExpand5 = GetOptionBool( "Game", "Is5ExpandShortcutWnd" );
    
    if (class'UIAPI_WINDOW'.static.IsShowWindow("AutoSS"))
    {
        if (!isVertical)
        {
            Me.SetWindowSize(108, 24);
            Me.SetAnchor("AutoSS", "TopRight", "TopLeft", 10, 0);            ////horizont
            Animation4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 30, 0);
            Shot4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 29, -1);
            texBG3.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 30, 0);
            return;
        }
        else
        {
            Me.SetWindowSize(24, 108);
            Me.SetAnchor("AutoSS", "BottomLeft", "TopLeft", 0, 10);          //// vertical
            Animation4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 0, 30);
            Shot4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , -1, 29);
            texBG3.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 0, 30);
            return;
        }
    }
    
    if (!isVertical)
    {
        Me.SetWindowSize(108, 24);
        Animation4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 30, 0);
        Shot4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 29, -1);
        texBG3.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 30, 0);
        if (IsExpand5)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndHorizontal_5", 36, -34);
        }   
        else if (IsExpand4)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndHorizontal_4", 36, -34);
        }
            
        else if (IsExpand3)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndHorizontal_3", 36, -34);
        }
        else if (IsExpand2)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndHorizontal_2", 36, -34);
        }
            
        else if (IsExpand1)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndHorizontal_1", 36, -34);
        }
            
        else
        {
            AnchorBSS("ShortcutWnd.ShortcutWndHorizontal", 36, -34);
        }
    }
    else
    {
        Me.SetWindowSize(24, 108);
        Animation4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 0, 30);
        Shot4.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , -1, 29);
        texBG3.SetAnchor("AutoBSS", "TopLeft", "TopLeft" , 0, 30);
        
        if (IsExpand5)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndVertical_5", -34, 36);
        }   
        else if (IsExpand4)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndVertical_4", -34, 36);
        }
            
        else if (IsExpand3)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndVertical_3", -34, 36);
        }
        else if (IsExpand2)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndVertical_2", -34, 36);
        }
            
        else if (IsExpand1)
        {
            AnchorBSS("ShortcutWnd.ShortcutWndVertical_1", -34, 36);
        }
            
        else
        {
            AnchorBSS("ShortcutWnd.ShortcutWndVertical", -34, 36);
        }
    }
    
}
function BeastSkillCast (string a_Param)
{
    local int AttackerID;
    local int SkillID;
    local int SkillLevel;
    local float SkillHitTime;
    local UserInfo PetInfo;
    local UserInfo AttackerInfo;
    local int SkillHitTime_ms;
    ParseInt(a_Param,"AttackerID",AttackerID);
    ParseInt(a_Param,"SkillID",SkillID);
    ParseInt(a_Param,"SkillLevel",SkillLevel);
    ParseFloat(a_Param,"SkillHitTime",SkillHitTime);
      
    if ( SkillHitTime > 0 )
        SkillHitTime_ms = int(SkillHitTime * 1000) + 300;
    else
        SkillHitTime_ms = 100;
    
      
    GetUserInfo(AttackerID,AttackerInfo);
    GetPlayerInfo(PetInfo);
      
    GetSkillInfo( SkillID, SkillLevel, UsedSkill );
    
    isAttacking = false;
    if (PetInfo.nID == AttackerID && !isBeastSoulSkill(SkillID) && !isBeastSpiritSkill(SkillID))
    {
        Me.KillTimer(1112);
        isUsedBeastSoul = false;
        isUsedBeastSpirit = false;
        Me.SetTimer(1112, SkillHitTime_ms + 50);
    
    }
    else if (PetInfo.nID == AttackerID && isBeastSoulSkill(SkillID))
    {
        isUsedBeastSoul = true;
        Me.KillTimer(1112);
        
    }
    else if    (PetInfo.nID == AttackerID && isBeastSpiritSkill(SkillID))
    {
        isUsedBeastSpirit = true;
        Me.KillTimer(1112);
        
    }   
}
function AttackBeast (string a_Param)
{
    local int AttackerID;
    local UserInfo PetInfo;
    local UserInfo AttackerInfo;
    
    ParseInt(a_Param,"AttackerID",AttackerID);
      
    GetUserInfo(AttackerID,AttackerInfo);
    GetPlayerInfo(PetInfo);
    
    isAttacking = true;
    
    if (PetInfo.nID == AttackerID)
    {
        Me.KillTimer(1112);
        isUsedBeastSoul = false;
        isUsedBeastSpirit = false;
        Me.SetTimer(1112, 50);
    }
}
function bool isBeastSoulSkill(int skillid)
{
    switch (skillid)
    {
        case 22036:
            return true;
        default:
            return false;
        break;
    }
}
function bool isBeastSpiritSkill(int skillid)
{
    switch (skillid)
    {
        case 22037:
        case 22038:
            return true;
        default:
            return false;
        break;
    }
}
function OnTimer(int TimerID)
{
    local UserInfo check;
    local UserInfo kek;
    
    if (GetPlayerInfo(check))
    {
        if (check.nCurHP <= 0)
        {
            Me.KillTimer(1112);   
            return;
        }
    }
    
    GetTargetInfo(kek);
    
    switch (TimerID)
    {
        case 1112:
            if (!isUsedBeastSoul && isExistBeastSoul && UsedSkill.IsMagic == 0 && (UsedSkill.IsDebuff || UsedSkill.OperateType == 0) )
            {
                InvItem.GetItem(BeastsoulIndex, BeastSoulShot);
                if (BeastSoulShot.ItemNum > IntToInt64(0))
                {
                    if (kek.nID > 0)
                    {
                        //RequestTargetUser(kek.nID);
                    }
                    RequestUseItem(BeastSoulShot.ID);
                }
                else
                {
                    StopThree();
                }
                
            }   
            if (!isUsedBeastSpirit && isExistBeastSpirit && !isAttacking && UsedSkill.IsMagic == 1)
            {
                InvItem.GetItem(BeastspiritIndex, BeastSpiritShot);
                if (BeastSpiritShot.ItemNum > IntToInt64(0))
                {
                    if (kek.nID > 0)
                    {
                        //RequestTargetUser(kek.nID);
                    }
                    RequestUseItem(BeastSpiritShot.ID);
                }
                else
                {
                    StopFour();
                }
                
            }
    Me.KillTimer(1112);   
    Me.SetTimer(1112, 50);
        break;
    }
}
function StartAnimThree() 
{
    Animation3.Stop();
    Animation3.SetLoopCount(-1);
    Animation3.Play();
}
function StartAnimFour() 
{
    Animation4.Stop();
    Animation4.SetLoopCount(-1);
    Animation4.Play();
}
function StopThree()
{
    Animation3.Stop();
    Shot3.Clear();
    isExistBeastSoul = false;
}
function StopFour()
{
    Animation4.Stop();
    Shot4.Clear();
    isExistBeastSpirit = false;
}
function FindAllBeastShots()
{
    local ItemID BeastSoulShotID;
    local ItemID BeastSpiritShotID;
    
    BeastSoulShotID.ClassID = 6645;
    BeastSpiritShotID.ClassID = 6647;////6646
    
    BeastsoulIndex = InvItem.FindItem( BeastSoulShotID );
    BeastspiritIndex = InvItem.FindItem( BeastSpiritShotID );
}
function EnterTheVoid2()
{       
     FindAllBeastShots();
        if (BeastsoulIndex != -1)
            AddBeastSoulShot(BeastsoulIndex);
        else
            StopThree();
    
        if (BeastspiritIndex != -1)
            AddBeastSpiritShot(BeastspiritIndex);   
        else
            StopFour();
}
function AddBeastSoulShot(int index)   
{
    local ItemInfo soulInfo;
    
    InvItem.GetItem(index, soulInfo);
    Shot3.Clear();
    Shot3.AddItem(soulInfo);
    StartAnimThree();
    RequestAutoBeastSoulShots(index);
    isExistBeastSoul = true;
}
function AddBeastSpiritShot(int index)   
{
    local ItemInfo spiritInfo;
    
    InvItem.GetItem(index, spiritInfo);
    Shot4.Clear();
    Shot4.AddItem(spiritInfo);
    StartAnimFour();
    RequestAutoBeastSoulShots(index);
    isExistBeastSpirit = true;
}
function RequestAutoBeastSoulShots(int index)
{
    if (index == BeastsoulIndex)
    {
        InvItem.GetItem(index, BeastSoulShot);
        RequestUseItem(BeastSoulShot.ID);
    }
    else if (index == BeastspiritIndex)
    {
        InvItem.GetItem(index, BeastSpiritShot);
        RequestUseItem(BeastSpiritShot.ID);
    }
}
function AnchorBSS(string window, int x, int y)
{
    Me.SetAnchor(window, "TopLeft", "TopLeft", x, y);
}
defaultproperties
{
}