//
// Generated by dtk
// Translation Unit: f_pc_priority.cpp
//

#include "f_pc/f_pc_priority.h"
#include "f_pc/f_pc_base.h"
#include "f_pc/f_pc_layer.h"
#include "f_pc/f_pc_layer_tag.h"

/* 8003FC08-8003FC28       .text fpcPi_IsInQueue__FP22process_priority_class */
s32 fpcPi_IsInQueue(process_priority_class* i_procPriority) {
    return cTg_IsUse(&i_procPriority->mBase);
}

/* 8003FC28-8003FC60       .text fpcPi_QueueTo__FP22process_priority_class */
s32 fpcPi_QueueTo(process_priority_class* i_procPriority) {
    cTg_SingleCut(&i_procPriority->mBase);
    fpcLy_CancelQTo(&i_procPriority->mMtdTag);
    return 1;
}

static node_list_class l_fpcPi_Queue;

/* 8003FC60-8003FCEC       .text fpcPi_ToQueue__FP22process_priority_class */
s32 fpcPi_ToQueue(process_priority_class* i_procPriority) {
    u32 layer = i_procPriority->mInfoQ.mLayer;

    if (cTg_Addition(&l_fpcPi_Queue, &i_procPriority->mBase)) {
        if (layer != fpcLy_CURRENT_e) {
            layer_class* pLayer = fpcLy_Layer(layer);

            if (!fpcLy_ToCancelQ(pLayer, &i_procPriority->mMtdTag)) {
                cTg_SingleCut(&i_procPriority->mBase);
                return 0;
            }
        }

        return 1;
    }

    return 0;
}

/* 8003FCEC-8003FD40       .text fpcPi_GetFromQueue__Fv */
process_priority_class* fpcPi_GetFromQueue() {
    process_priority_class* i_procPriority = (process_priority_class*)cTg_GetFirst(&l_fpcPi_Queue);

    if (i_procPriority != NULL) {
        base_process_class* pProc = (base_process_class*)i_procPriority->mBase.mpTagData;
        process_priority_class* pProcPi = &pProc->mPi;
        fpcLy_CancelQTo(&pProcPi->mMtdTag);
        return pProcPi;
    }

    return NULL;
}

/* 8003FD40-8003FD8C       .text fpcPi_Delete__FP22process_priority_class */
s32 fpcPi_Delete(process_priority_class* i_procPriority) {
    static process_priority_queue_info crear = {
        0xFFFFFFFF,
        0xFFFF,
        0xFFFF,
    };

    fpcPi_QueueTo(i_procPriority);
    i_procPriority->mInfoQ.mLayer = crear.mLayer;
    i_procPriority->mInfoQ.mListID = crear.mListID;
    i_procPriority->mInfoQ.mListPrio = crear.mListPrio;
    return 1;
}

/* 8003FD8C-8003FDC0       .text fpcPi_IsNormal__FUiUsUs */
s32 fpcPi_IsNormal(unsigned int i_layer, u16 i_listID, u16 i_priority) {
    if ((i_layer < fpcLy_SPECIAL_e) && (i_listID < fpcPi_SPECIAL_e) && (i_priority < fpcPi_SPECIAL_e))
        return 1;

    return 0;
}

/* 8003FDC0-8003FF00       .text fpcPi_Change__FP22process_priority_classUiUsUs */
s32 fpcPi_Change(process_priority_class* i_procPriority, unsigned int i_layer, u16 i_listID, u16 i_priority) {
    base_process_class* pProc = (base_process_class*)i_procPriority->mBase.mpTagData;
    BOOL changed = 0;

    if (pProc->mInitState == 3)
        return 0;

    if (!fpcPi_IsNormal(i_layer, i_listID, i_priority))
        return 0;

    i_procPriority->mInfoQ.mLayer = i_procPriority->mInfoCurr.mLayer;
    i_procPriority->mInfoQ.mListID = i_procPriority->mInfoCurr.mListID;
    i_procPriority->mInfoQ.mListPrio = i_procPriority->mInfoCurr.mListPrio;

    if (i_layer != fpcLy_CURRENT_e && i_procPriority->mInfoCurr.mLayer != i_layer) {
        i_procPriority->mInfoQ.mLayer = i_layer;
        changed = 1;
    }

    if (i_listID != fpcPi_CURRENT_e && i_procPriority->mInfoCurr.mListID != i_listID) {
        i_procPriority->mInfoQ.mListID = i_listID;
        changed = 1;
    }

    if (i_priority != fpcPi_CURRENT_e && i_procPriority->mInfoCurr.mListPrio != i_priority) {
        i_procPriority->mInfoQ.mListPrio = i_priority;
        changed = 1;
    }

    if (pProc->mInitState == 0 || pProc->mInitState == 1) {
        i_procPriority->mInfoCurr.mLayer = i_procPriority->mInfoQ.mLayer;
        i_procPriority->mInfoCurr.mListID = i_procPriority->mInfoQ.mListID;
        i_procPriority->mInfoCurr.mListPrio = i_procPriority->mInfoQ.mListPrio;
        return 1;
    }

    if (changed == 1)
        return fpcPi_ToQueue(i_procPriority);
    else
        return 0;
}

/* 8003FF00-8003FF94       .text fpcPi_Handler__Fv */
s32 fpcPi_Handler() {
    process_priority_class* i_procPriority;
    while (i_procPriority = fpcPi_GetFromQueue()) {
        base_process_class* pProc = (base_process_class*)i_procPriority->mBase.mpTagData;
        layer_management_tag_class* pLayerTag = &pProc->mLyTg;
        line_tag* pLineTag = &pProc->mLnTg;
        if (fpcLyTg_Move(pLayerTag, i_procPriority->mInfoQ.mLayer, i_procPriority->mInfoQ.mListID, i_procPriority->mInfoQ.mListPrio) == 1) {
            fpcLnTg_Move(pLineTag, i_procPriority->mInfoCurr.mListID);
            i_procPriority->mInfoCurr.mLayer = i_procPriority->mInfoQ.mLayer;
            i_procPriority->mInfoCurr.mListID = i_procPriority->mInfoQ.mListID;
            i_procPriority->mInfoCurr.mListPrio = i_procPriority->mInfoQ.mListPrio;
        } else {
            return 0;
        }
    }

    return 1;
}

/* 8003FF94-8004003C       .text fpcPi_Init__FP22process_priority_classPvUiUsUs */
s32 fpcPi_Init(process_priority_class* i_procPriority, void* i_data, unsigned int i_layer, u16 i_listID,
               u16 i_priority) {
    if (!fpcPi_IsNormal(i_layer, i_listID, i_priority))
        return 0;

    i_procPriority->mInfoQ.mLayer = i_layer;
    i_procPriority->mInfoQ.mListID = i_listID;
    i_procPriority->mInfoQ.mListPrio = i_priority;

    i_procPriority->mInfoCurr.mLayer = i_procPriority->mInfoQ.mLayer;
    i_procPriority->mInfoCurr.mListID = i_procPriority->mInfoQ.mListID;
    i_procPriority->mInfoCurr.mListPrio = i_procPriority->mInfoQ.mListPrio;
    cTg_Create(&i_procPriority->mBase, i_data);
    fpcMtdTg_Init(&i_procPriority->mMtdTag, (process_method_tag_func)fpcPi_Delete, i_procPriority);
    return 1;
}
