#include "Arb_State.h"
#include "Arb.h"

/**
 * @brief 通过ID获取对应板卡区域状态
 */
ENUM_AREA_STATE AreaArb_GetState_byID(CM_UINT8 ID)
{
    ENUM_AREA_STATE state = ENUM_AREA_SHUTDOWN;
    if(ID == AREA_BASIC_CFG_PTR->BoardID)
    {
        state = AREA_STATE_CTRL_PTR->StateArea.Local_state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_BoardID)
    {
        state = AREA_STATE_CTRL_PTR->StateArea.Partner_state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_A_BoardID)
    {
        state = AREA_STATE_CTRL_PTR->StateArea.Partner_Area_A_state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_B_BoardID)
    {
        state = AREA_STATE_CTRL_PTR->StateArea.Partner_Area_B_state;
    }
    else
    {
        AREA_LOG_MSG("AREA_STATE:Get state failed, invalid board ID:%d\n", ID);
    }
    return state;
}

/**
 * @brief 通过ID设置对应板卡区域状态
 */
void AreaArb_SetState_byID(CM_UINT8 ID, ENUM_AREA_STATE state)
{
    if((state < ENUM_AREA_SHUTDOWN) || (state > ENUM_AREA_SLAVE))
    {
        AREA_LOG_MSG("AREA_STATE:Set invalid area state, ID:%d State:%d\n", ID, state);
    }

    if(ID == AREA_BASIC_CFG_PTR->BoardID)
    {
        AREA_STATE_CTRL_PTR->StateArea.Local_state = state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_BoardID)
    {
        AREA_STATE_CTRL_PTR->StateArea.Partner_state = state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_A_BoardID)
    {
        AREA_STATE_CTRL_PTR->StateArea.Partner_Area_A_state = state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_B_BoardID)
    {
        AREA_STATE_CTRL_PTR->StateArea.Partner_Area_B_state = state;
    }
    else
    {
        AREA_LOG_MSG("AREA_STATE:Set state failed, invalid board ID:%d State:%d\n", ID, state);
    }
}

/**
 * @brief 通过ID获取对应板卡2x状态
 */
ENUM_2x2_STATE_T AreaArb_Get2xState_byID(CM_UINT8 ID)
{
    ENUM_2x2_STATE_T state = ENUM_2x2_SHUTDOWN;
    if(ID == AREA_BASIC_CFG_PTR->BoardID)
    {
        state = AREA_STATE_CTRL_PTR->State2x.LocalState;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_BoardID)
    {
        state = AREA_STATE_CTRL_PTR->State2x.PartnerState;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_A_BoardID)
    {
        state = AREA_STATE_CTRL_PTR->State2x.Area_A_State;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_B_BoardID)
    {
        state = AREA_STATE_CTRL_PTR->State2x.Area_B_State;
    }
    else
    {
        AREA_LOG_MSG("AREA_STATE:Get 2x2 state failed, invalid board ID:%d\n", ID);
    }
    return state;
}

/**
 * @brief 通过ID设置对应板卡2x状态
 */
void AreaArb_Set2xState_byID(CM_UINT8 ID, ENUM_2x2_STATE_T state)
{
    if((state < ENUM_2x2_SHUTDOWN) || (state > ENUM_2x2_MASTER))
    {
        AREA_LOG_MSG("AREA_STATE:Set invalid 2x2 state, ID:%d State:%d\n", ID, state);
    }

    if(ID == AREA_BASIC_CFG_PTR->BoardID)
    {
        AREA_STATE_CTRL_PTR->State2x.LocalState = state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_BoardID)
    {
        AREA_STATE_CTRL_PTR->State2x.PartnerState = state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_A_BoardID)
    {
        AREA_STATE_CTRL_PTR->State2x.Area_A_State = state;
    }
    else if(ID == AREA_BASIC_CFG_PTR->Partner_Area_B_BoardID)
    {
        AREA_STATE_CTRL_PTR->State2x.Area_B_State = state;
    }
    else
    {
        AREA_LOG_MSG("AREA_STATE:Set 2x2 state failed, invalid board ID:%d State:%d\n", ID, state);
    }
}
/**
 * @brief 通过状态获取某个板卡ID
 */
CM_UINT8 AreaArb_GetID_byState(ENUM_AREA_STATE state_area, ENUM_2x2_STATE_T state_2x)
{
    CM_UINT8 boardID = 0u;

    if(AREA_STATE_CTRL_PTR->StateArea.Local_state == state_area && AREA_STATE_CTRL_PTR->State2x.LocalState == state_2x)
    {
        boardID = AREA_BASIC_CFG_PTR->BoardID;
    }
    else if(AREA_STATE_CTRL_PTR->StateArea.Partner_state == state_area && AREA_STATE_CTRL_PTR->State2x.PartnerState == state_2x)
    {
        boardID = AREA_BASIC_CFG_PTR->Partner_BoardID;
    }
    else if(AREA_STATE_CTRL_PTR->StateArea.Partner_Area_A_state == state_area && AREA_STATE_CTRL_PTR->State2x.Area_A_State == state_2x)
    {
        boardID = AREA_BASIC_CFG_PTR->Partner_Area_A_BoardID;
    }
    else if(AREA_STATE_CTRL_PTR->StateArea.Partner_Area_B_state == state_area && AREA_STATE_CTRL_PTR->State2x.Area_B_State == state_2x)
    {
        boardID = AREA_BASIC_CFG_PTR->Partner_Area_B_BoardID;
    }
    else
    {
        AREA_LOG_MSG("AREA_STATE:Board not found by state, Area:%d 2x2:%d\n", state_area, state_2x);
    }

    return boardID;
}
/**
 * @brief 仲裁模式下使用，获取所有区域状态
 */
void AreaArb_Get_All_State(STR_Area_State *p_all_State)
{
	if(NULL == p_all_State)
	{
		AREA_LOG_MSG("AREA_STATE:Get all states failed, output is NULL\n");
	}
	else
	{
		p_all_State->Local_state   =  AREA_STATE_CTRL_PTR->StateArea.Local_state;
		p_all_State->Partner_state =  AREA_STATE_CTRL_PTR->StateArea.Partner_state;
		p_all_State->Partner_Area_A_state =  AREA_STATE_CTRL_PTR->StateArea.Partner_Area_A_state;
		p_all_State->Partner_Area_B_state =  AREA_STATE_CTRL_PTR->StateArea.Partner_Area_B_state;
	}
}
/**
 * @brief 判断是否存在备地
 */
CM_BOOL AreaArb_Slave_Is_Exist()
{
	CM_BOOL rt = CM_FALSE;
	if((ENUM_AREA_SLAVE == AREA_STATE_CTRL_PTR->StateArea.Local_state) || (ENUM_AREA_SLAVE == AREA_STATE_CTRL_PTR->StateArea.Partner_state)
			|| (ENUM_AREA_SLAVE == AREA_STATE_CTRL_PTR->StateArea.Partner_Area_A_state) || (ENUM_AREA_SLAVE == AREA_STATE_CTRL_PTR->StateArea.Partner_Area_B_state))
	{
		rt = CM_TRUE;
	}
	else
	{

	}
	return rt;
}