#include "MyLockonController.h"

#include "LockonCursor001_Main.h"
#include "LockonCursor001_Sub.h"
#include "jp/gecchi/VioletVreath/actor/my/MyLockonController.h"

using namespace GgafLib;
using namespace VioletVreath;

/** 侾僆僾僔儑儞摉偨傝偺嵟戝壜擻儘僢僋僆儞悢 */
const int MyLockonController::max_lockon_num_ = 8;
int MyLockonController::lockon_num_ = 0;

MyLockonController::MyLockonController(const char* prm_name) :
        GgafCore::DestructActor(prm_name) {
    _class_name = "MyLockonController";
    pMainLockonEffect_ = NEW LockonCursor001_Main("MAIN");
    pMainLockonEffect_->inactivate();
    appendChild(pMainLockonEffect_);
    for (int i = 1; i < MyLockonController::max_lockon_num_; i++) {
        std::string name = "ChildLockon("+XTOS(i)+")";
        LockonCursor001_Sub* pLockonCursor_Sub = NEW LockonCursor001_Sub(name.c_str());
        pLockonCursor_Sub->inactivate();
        appendChild(pLockonCursor_Sub);
    }
}

void MyLockonController::initialize() {
}
void MyLockonController::onReset() {
    //儘僢僋僆儞僞乕僎僢僩偺儕僗僩傪嬻偵偡傞
    listTarget_.removeAll();
    //儘僢僋僆儞傾僋僞乕偺儕僙僢僩
    LockonCursor001* pLockonCursor001;
    for (int i = 0; i < MyLockonController::max_lockon_num_; i++) {
        pLockonCursor001 = (LockonCursor001*)(getChild(i));
        pLockonCursor001->releaseLockon();
        pLockonCursor001->inactivate();
    }
}

void MyLockonController::onActive() {
    onReset();
}


void MyLockonController::processBehavior() {
    //儘僢僋僆儞僞乕僎僢僩惗懚妋擣
    GgafDx::GeometricActor* pMainLockonTarget = listTarget_.getCurrent(); //儊僀儞儘僢僋僆儞偺僞乕僎僢僩
    GgafDx::GeometricActor* pTarget = listTarget_.getCurrent();  //僞乕僎僢僩僇乕僜儖
    GgafCore::MainActor* pLockonEffect_Active = (GgafCore::MainActor*)getChildFirst();         //儘僢僋僆儞僄僼僃僋僩僇乕僜儖
    int n = listTarget_.length();
    for (int i = 0; i < n; i++) {
        if (pTarget->isActiveInTheTree() && pTarget->getStatus()->get(STAT_Stamina) > 0) {
            //僞乕僎僢僩偑懚柦側傜偽
            //僄僼僃僋僩傾僋僞乕偺僞乕僎僢僩峏怴丄僄僼僃僋僩傪僘儖偭偲偟傑偡
            ((LockonCursor001*)pLockonEffect_Active)->lockon(pTarget);
            if (!pLockonEffect_Active->isActiveInTheTree()) {
                pLockonEffect_Active->activate();
            }
            pLockonEffect_Active = pLockonEffect_Active->getNext(); //師夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕師傊
            pTarget = listTarget_.next();                         //師夞張棟僞乕僎僢僩傪師傊
        } else {
            //僞乕僎僢僩偑巰朣帪
            if (pTarget == pMainLockonTarget) {
                //巰朣僞乕僎僢僩偑儊僀儞儘僢僋僆儞偺僞乕僎僢僩偱偁偭偨応崌
                if (listTarget_.length() == 1) {
                    //巰朣僞乕僎僢僩偑儊僀儞儘僢僋僆儞偺僞乕僎僢僩偱儕儞僌偺嵟屻偺堦偮偺応崌
                    listTarget_.remove();                                    //僞乕僎僢僩敳偒弌偟
                    ((LockonCursor001*)pLockonEffect_Active)->releaseLockon(); //儘僢僋僆儞僄僼僃僋僩傪儕儕乕僗幚峴
                    pTarget = nullptr;
                    break;
                } else {
                    //巰朣僞乕僎僢僩偑儊僀儞儘僢僋僆儞偺僞乕僎僢僩偱儕儞僌偺嵟屻偺堦偮偱偼柍偄応崌
                    listTarget_.remove();               //僞乕僎僢僩敳偒弌偟
                    pTarget = listTarget_.getCurrent(); //師夞張棟僞乕僎僢僩傪師傊乮remove()偵傛傝師偺僞乕僎僢僩偼getCurrent()偲側傞乯
                    pMainLockonTarget = pTarget;          //儊僀儞儘僢僋僆儞偺僞乕僎僢僩傪峏怴
                    //儊僀儞儘僢僋僆儞僄僼僃僋僩傪捈嬤儘僢僋僆儞傊栠偡偟
                    //儊僀儞儘僢僋僆儞僄僼僃僋僩偺師偺僄僼僃僋僩傪夝曻偡傞偲偄偆摦嶌傪偟偨偄
                    ((LockonCursor001*)pLockonEffect_Active->getNext())->releaseLockon(); //乽師乿偺儘僢僋僆儞僄僼僃僋僩傪儕儕乕僗幚峴
                    pLockonEffect_Active->getNext()->moveLast();               //師偺儘僢僋僆儞僄僼僃僋僩傪枛旜傊
                    //乽師夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕師傊乿偺張棟懳徾堏摦偼晄梫丅師偵張棟偟偨偄儘僢僋僆儞僄僼僃僋僩傾僋僞乕偦偺傑傑偱偁傞偨傔丅
                }
            } else {
                //巰朣僞乕僎僢僩偑僒僽儘僢僋僆儞偺僞乕僎僢僩偱偁偭偨応崌
                listTarget_.remove();               //僞乕僎僢僩敳偒弌偟
                pTarget = listTarget_.getCurrent(); //師夞張棟僞乕僎僢僩傪師傊乮remove()偵傛傝師偺僞乕僎僢僩偼getCurrent()偲側傞乯
                ((LockonCursor001*)pLockonEffect_Active)->releaseLockon(); //儘僢僋僆儞僄僼僃僋僩傪儕儕乕僗幚峴
                pLockonEffect_Active = pLockonEffect_Active->getNext();    //師夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕師傊
                pLockonEffect_Active->getPrev()->moveLast();    //崱夞張棟儘僢僋僆儞僄僼僃僋僩傾僋僞乕傪枛旜傊
            }
        }
    }
}

void MyLockonController::processJudgement() {
}

void MyLockonController::onInactive() {
    onReset();
}

void MyLockonController::lockon(GgafDx::GeometricActor* prm_pTarget) {
    if (MyLockonController::lockon_num_ == 0) {
        return; //尰嵼偺儘僢僋僆儞壜擻悢偑0
    }

    if (listTarget_.indexOf(prm_pTarget) == -1) {
        //儘僢僋僆儞嵪傒偵柍偄偺偱丄儘僢僋僆儞偡傞
        if (listTarget_.length() >= MyLockonController::lockon_num_) { //僞乕僎僢僩儕僗僩偑婛偵枮堳偺応崌
            //儘僢僋僆儞僞乕僎僢僩儘乕僥乕僩
            listTarget_.prev();           //僇儗儞僩傪嵟廔梫慺傊
            listTarget_.set(prm_pTarget, false); //嵟廔梫慺偩偭偨偲偙傠偵儊僀儞僞乕僎僢僩忋彂偒
            //儘僢僋僆儞僄僼僃僋僩傾僋僞乕偼偦偺傑傑
            //偙傟偵傛傝丄processBehavior() 偺僞乕僎僢僩懚柦帪偺偟傚傝偵傛傝
            //僘儖僢偲偢傟傞丅
        } else {   //傑偩僞乕僎僢僩儕僗僩偵壗傕側偄丄埥偄偼梋桾偑偁傞乮捛壛壜擻側応崌乯
            //僞乕僎僢僩儘乕僥乕僩
            listTarget_.addPrev(prm_pTarget, false);
            listTarget_.prev();
            //儘僢僋僆儞僄僼僃僋僩
            if (listTarget_.length() == 1) {
                //嵟弶偺儘僢僋僆儞僞乕僎僢僩捛壛帪乮儊僀儞儘僢僋僆儞僞乕僎僢僩乯
                GgafCore::MainActor* pLockonEffect = (GgafCore::MainActor*)getChildFirst(); //儊僀儞儘僢僋僆僄僼僃僋僩
                pLockonEffect->activate();
                ((LockonCursor001*)pLockonEffect)->lockon(prm_pTarget);

                //嵟弶偺儘僢僋僆儞僞乕僎僢僩埲奜偺捛壛帪乮僒僽儘僢僋僆儞僞乕僎僢僩捛壛帪乯
            } else if (listTarget_.length() > 1) {
                //Child儘僢僋僆儞捛壛帪
                if (listTarget_.length() >= 3) {
                    //俁屄栚偺埲崀僞乕僎僢僩捛壛帪乮俀屄栚埲崀偺僒僽儘僢僋僆儞僞乕僎僢僩捛壛帪乯
                    //儘僢僋僆儞僄僼僃僋僩偺摿庩側儘乕僥乕僩傪峴偆丅乮仸嵟屻偺僐儊儞僩亙捛壛偺応崌亜嶲徠乯
                    //Last傪愗傝弌偡
                    GgafCore::MainActor* pLockonEffect = (GgafCore::MainActor*)getChildFirst()->getPrev(); //Last
                    GgafCore::MainActor* pLockonEffect_Next = pLockonEffect->getNext(); //Main儘僢僋僆儞偲側傞
                    GgafCore::MainActor* pLockonEffect_Prev = pLockonEffect->getPrev();
                    pLockonEffect_Prev->_pNext = pLockonEffect_Next;
                    pLockonEffect_Next->_pPrev = pLockonEffect_Prev;
                    pLockonEffect_Prev->_is_last_flg = true;
                    pLockonEffect->_is_last_flg = false;
                    //First->Next 偺娫偵擖傟傞
                    GgafCore::MainActor* pMainLockonEffect = (GgafCore::MainActor*)getChildFirst();
                    GgafCore::MainActor* pMainLockonEffect_Next = (GgafCore::MainActor*)getChildFirst()->getNext();
                    pMainLockonEffect->_pNext = pLockonEffect;
                    pLockonEffect->_pPrev = pMainLockonEffect;
                    pLockonEffect->_pNext = pMainLockonEffect_Next;
                    pMainLockonEffect_Next->_pPrev = pLockonEffect;
                    pLockonEffect->activate(); //僒僽儘僢僋僆儞桳岠偵
                    //僒僽儘僢僋僆儞僄僼僃僋僩儘僢僋僆儞両
                    ((LockonCursor001*)pLockonEffect)->lockon(listTarget_.getNext());
                } else {
                    //俀屄栚偺僞乕僎僢僩捛壛帪乮嵟弶偺僒僽儘僢僋僆儞僞乕僎僢僩捛壛帪乯
                    GgafCore::MainActor* pLockonEffect = (GgafCore::MainActor*)getChildFirst()->getPrev(); //俀偮側偺偱寢嬊Next偺埵抲
                    pLockonEffect->activate(); //僒僽儘僢僋僆儞桳岠偵
                    ((LockonCursor001*)pLockonEffect)->lockon(listTarget_.getNext());
                }
            }
        }
    } else {
        //prm_pTarget 偼婛偵儘僢僋僆儞嵪傒
    }
}

void MyLockonController::releaseAllLockon() {
    if (listTarget_.length() == 0) {
        return;
    }
    listTarget_.removeAll();
    //儘僢僋僆儞僄僼僃僋僩傪僀儞傾僋僥傿僽偵偡傞
    GgafCore::MainActor* pLockonEffect = (GgafCore::MainActor*)getChildFirst();
    while (pLockonEffect) {
        ((LockonCursor001*)pLockonEffect)->releaseLockon();
        if (pLockonEffect->isLast()) {
            break;
        } else {
            pLockonEffect = pLockonEffect->getNext();
        }
    }
}

MyLockonController::~MyLockonController() {
}

void MyLockonController::dumpTarget(GgafDx::GeometricActor* pMain) {
    if (listTarget_.getFromFirst(0) == nullptr) {
        _TRACE_N_("nullptr\n");
        return;
    } else {
        _TRACE_N_("佁");
    }
    for (int i = 0; i < listTarget_.length(); i++) {
        if (listTarget_.getFromFirst(i) == pMain) {
            _TRACE_N_("両");
        }
        _TRACE_N_(listTarget_.getFromFirst(i)->getName()<<"["<<i<<"]佁");
    }
    _TRACE_N_(" ... avtive="<<listTarget_.getCurrent()->getName()<<"\n");
}

//亂旛朰榐亃
//乽儘僢僋僆儞僞乕僎僢僩(揋)偲儘僢僋僆儞僄僼僃僋僩(徠弨)偺娭學乿(2010/09/14)
//
//儘僢僋僆儞帪丄儘僢僋僆儞儕儕乕僗帪偺
//儘僢僋僆儞僞乕僎僢僩偲儘僢僋僆儞僄僼僃僋僩偺娭學傪儊儌偲偟偰巆偡丅
//
//俵      乧 儊僀儞儘僢僋僆儞僄僼僃僋僩
//S0乣S3  乧 僒僽儘僢僋僆儞僄僼僃僋僩
//t1乣t5  乧 儘僢僋僆儞僞乕僎僢僩
//
//儊僀儞儘僢僋僆儞僄僼僃僋僩(俵)偺応強偵埵抲偡傞儘僢僋僆儞僞乕僎僢僩傪
//乽儊僀儞儘僢僋僆儞僞乕僎僢僩乿
//僒僽儘僢僋僆儞僄僼僃僋僩(S0乣S3)偺応強偵埵抲偡傞儘僢僋僆儞僞乕僎僢僩傪
//乽僒僽儘僢僋僆儞僞乕僎僢僩乿
//偲屇傇偙偲偵偡傞丅

//亙捛壛偺応崌亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
//          伀            t1儘僢僋僆儞
// 佁t1佁
// 僄僼僃僋僩傾僋僞乕曄壔柍偟
//          伀            t2捛壛儘僢僋僆儞
// 佁t2佁t1佁
// 佁俵佁S3佁S0佁S1佁S2佁
//          伀            t3捛壛儘僢僋僆儞
// 佁t3佁t2佁t1佁
// 佁俵佁S2佁S3佁S0佁S1佁
//          伀            t4捛壛儘僢僋僆儞
// 佁t4佁t3佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁
//          伀            t5捛壛儘僢僋僆儞
// 佁t5佁t4佁t3佁t2佁t1佁
// 佁俵佁S0佁S1佁S2佁S3佁

// 亙枮堳偱捛壛偺応崌亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕偼憖嶌晄梫
//
// 佁t5佁t4佁t3佁t2佁t1佁
// 佁t6佁t5佁t4佁t3佁t2佁   僘儖偭
// 佁t7佁t6佁t5佁t4佁t3佁   僘儖偭
// 佁t8佁t7佁t6佁t5佁t4佁   僘儖偭
// 佁t9佁t8佁t7佁t6佁t5佁   僘儖偭偲偟傑偡

//亙巰朣偺応崌亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t1)
// 佁t5佁t4佁t3佁t2佁
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒曄壔柍偟乮S3偑枛旜傊乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t2)
// 佁t5佁t4佁t3佁t1佁
// 佁俵佁S0佁S1佁S3佁S2佁    丒丒丒S2偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t3)
// 佁t5佁t4佁t2佁t1佁
// 佁俵佁S0佁S2佁S3佁S1佁    丒丒丒S1偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t4)
// 佁t5佁t3佁t2佁t1佁
// 佁俵佁S2佁S3佁S1佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t5佁t4佁t3佁t2佁t1佁
//          伀    remove(t5)
// 佁t4佁t3佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒 S0偑枛旜傊乮拲堄乯堏摦偟偰lockonRelase()



//亙愗傟傞応崌2亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t1)
// 佁t4佁t3佁t1佁
// 佁俵佁S0佁S1佁S3佁S2佁    丒丒丒S2偑枛旜傊乮張棟徣棯壜丠乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t2)
// 佁t4佁t3佁t1佁
// 佁俵佁S0佁S2佁S3佁S1佁    丒丒丒S1偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t3)
// 佁t4佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t4佁t3佁t2佁t1佁
//          伀    remove(t4)
// 佁t3佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊乮拲堄乯堏摦偟偰lockonRelase()



//亙愗傟傞応崌3亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t3佁t2佁t1佁
//          伀    remove(t1)
// 佁t3佁t2佁
// 佁俵佁S0佁S2佁S3佁S1佁    丒丒丒S1偑枛旜傊乮張棟徣棯壜丠乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t3佁t2佁t1佁
//          伀    remove(t2)
// 佁t3佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t3佁t2佁t1佁
//          伀    remove(t3)
// 佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊堏摦偟偰lockonRelase()



//亙愗傟傞応崌4亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t2佁t1佁
//          伀    remove(t1)
// 佁t2佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜傊乮張棟徣棯壜丠乯堏摦偟偰lockonRelase()

// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t2佁t1佁
//          伀    remove(t2)
// 佁t2佁t1佁
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒S0偑枛旜僿堏摦偟偰lockonRelase()


//亙愗傟傞応崌5亜
// 佁俵佁S0佁S1佁S2佁S3佁    丒丒丒僄僼僃僋僩傾僋僞乕
// 佁t1佁
//          伀    remove(t1)
//
// 佁俵佁S1佁S2佁S3佁S0佁    丒丒丒曄壔柍偟偱M傪lockonRelase()
