#include "common_include.h"
#include "app_service.h"

SERVICE ServicePara = {0};

void USMServiceIntervalSettingFunc(uint8_t Key)
{
    if(ServicePara.Flag_OK == 1 || ServicePara.Flag_Cancel == 1)
    {
        ServicePara.Flag_Confirm = 0;
        ServicePara.Flag = 0;
        ServicePara.Flag_OK = 0;
        ServicePara.Flag_Cancel = 0;
    }
    switch(Key)
    {
    case TRIP_SW1_CUR_DOWN:
        if(ServicePara.Flag_Confirm == 0)
        {
            if(ServicePara.SetNum > 0)
            {
                ServicePara.SetNum--;
            }
            else
            {
                ServicePara.SetNum = 9;
            }
            if(ServicePara.SetBit == 0)
            {
                ServicePara.SetTempKm = ServicePara.SetTempKm % 10000 + ServicePara.SetNum * 10000;
            }
            else if(ServicePara.SetBit == 1)
            {
                ServicePara.SetTempKm = ServicePara.SetTempKm / 10000 * 10000 + ServicePara.SetTempKm % 1000 + ServicePara.SetNum * 1000;
            }
            else if(ServicePara.SetBit == 2)
            {
                ServicePara.SetTempKm = ServicePara.SetTempKm / 1000 * 1000 + ServicePara.SetTempKm % 100 + ServicePara.SetNum * 100;
            }
            else if(ServicePara.SetBit == 3)
            {
                ServicePara.SetTempMonth = ServicePara.SetTempMonth % 10 + ServicePara.SetNum * 10;
            }
            else if(ServicePara.SetBit == 4)
            {
                ServicePara.SetTempMonth = ServicePara.SetTempMonth / 10 * 10 + ServicePara.SetNum;
            }
        }
        else
        {
            if(ServicePara.SetNum == 0)
            {
                ServicePara.SetNum = 1;
                USMSelectMovePoint = -32;
            }
            else
            {
                ServicePara.SetNum = 0;
            }
        }
        break;
    case TRIP_SW1_CUR_UP:
        if(ServicePara.Flag_Confirm == 0)
        {
            if(ServicePara.SetNum < 9)
            {
                ServicePara.SetNum++;
            }
            else
            {
                ServicePara.SetNum = 0;
            }
            if(ServicePara.SetBit == 0)
            {
                ServicePara.SetTempKm = ServicePara.SetTempKm % 10000 + ServicePara.SetNum * 10000;
            }
            else if(ServicePara.SetBit == 1)
            {
                ServicePara.SetTempKm = ServicePara.SetTempKm / 10000 * 10000 + ServicePara.SetTempKm % 1000 + ServicePara.SetNum * 1000;
            }
            else if(ServicePara.SetBit == 2)
            {
                ServicePara.SetTempKm = ServicePara.SetTempKm / 1000 * 1000 + ServicePara.SetTempKm % 100 + ServicePara.SetNum * 100;
            }
            else if(ServicePara.SetBit == 3)
            {
                ServicePara.SetTempMonth = ServicePara.SetTempMonth % 10 + ServicePara.SetNum * 10;
            }
            else if(ServicePara.SetBit == 4)
            {
                ServicePara.SetTempMonth = ServicePara.SetTempMonth / 10 * 10 + ServicePara.SetNum;
            }
        }
        else
        {
            if(ServicePara.SetNum == 1)
            {
                ServicePara.SetNum = 0;
                USMSelectMovePoint = 32;
            }
            else
            {
                ServicePara.SetNum = 1;
            }
        }
        break;
    case TRIP_SW1_CUR_OK:
        if(ServicePara.Flag_Confirm == 0)
        {
            if(ServicePara.SetBit < 4)
            {
                ServicePara.SetBit++;
                if(ServicePara.SetBit == 0)
                {
                    ServicePara.SetNum = ServicePara.SetTempKm / 10000;
                }
                else if(ServicePara.SetBit == 1)
                {
                    ServicePara.SetNum = ServicePara.SetTempKm % 10000 / 1000;
                }
                else if(ServicePara.SetBit == 2)
                {
                    ServicePara.SetNum = ServicePara.SetTempKm % 1000 / 100;
                }
                else if(ServicePara.SetBit == 3)
                {
                    ServicePara.SetNum = ServicePara.SetTempMonth / 10;
                }
                else if(ServicePara.SetBit == 4)
                {
                    ServicePara.SetNum = ServicePara.SetTempMonth % 10;
                }
            }
            else
            {
                if(ServicePara.SetSaveKm != ServicePara.SetTempKm || ServicePara.SetSaveMonth != ServicePara.SetTempMonth)
                {
                    ServicePara.Flag_Confirm = 1;
                    ServicePara.SetNum = 0;
                }
                else
                {
                    ServicePara.Flag = 0;
                }
            }
        }
        else
        {
            if(ServicePara.SetNum == 0)
            {
                ServicePara.ServiceValid = SERVICE_REMAINDER_OFF;
                ServicePara.SetTempKm = ServicePara.SetTempKm - ServicePara.SetTempKm % 100;
                ServicePara.SetSaveKm = ServicePara.SetTempKm;
                ServicePara.SetSaveMonth = ServicePara.SetTempMonth;
                if(ServicePara.SetSaveKm != 0)
                {
                    ServicePara.ServiceValid |= SERVICE_REMAINDER_VALID_KM;
                }
                if(ServicePara.SetSaveMonth != 0)
                {
                    ServicePara.ServiceValid |= SERVICE_REMAINDER_VALID_TIME;
                }
                ServicePara.RemainKm = ServicePara.SetTempKm;
                ServicePara.RemainDay = ServicePara.SetTempMonth * 365 / 12;
                ServicePara.Flag_OK = 1;
                ServicePara.Flag_Confirm = 0;
                ServicePara.RemainSeconds = (int32_t)ServicePara.RemainDay * 24 * 3600;
                SaveServiceRemainderValid = 2;
            }
            else
            {
                ServicePara.Flag_Confirm = 0;
                ServicePara.Flag_Cancel = 1;
            }
        }
        break;
    case TRIP_SW1_CUR_MENU:
        if(ServicePara.Flag_Confirm == 0)
        {
            if(ServicePara.SetTempKm == ServicePara.SetSaveKm && ServicePara.SetTempMonth == ServicePara.SetSaveMonth)
            {
                ServicePara.Flag = 0;
            }
            else
            {
                ServicePara.Flag_Confirm = 1;
                ServicePara.SetNum = 0;
            }
            while(GroupTabPara.GroupTabUSM.CurDepth > 0)
            {
                GroupTabPara.GroupTabUSM.CurDepthSelect[GroupTabPara.GroupTabUSM.CurDepth] = 0;
                uint8_t tmp = FindUSMID(USM_OP_BACKWARD);
                if (tmp != 0xff) 
                {
                    USMChange(GroupTabPara.GroupTabUSM.CurDepth, tmp, USM_OP_BACKWARD);
                }
            }
            GroupTabPara.GroupTabUSM.LastDepth = GroupTabPara.GroupTabUSM.CurDepth;
            LastUSMTransitionTime = 0;
        }
        break;
    }
}

void USMServiceResetFunc(uint8_t Key)
{
    switch(Key)
    {
    case TRIP_SW1_CUR_DOWN:
        if(ServiceResetSetNum == 0)
        {
            ServiceResetSetNum = 1;
            USMSelectMovePoint = -32;
        }
        else
        {
            ServiceResetSetNum = 0;
        }
        break;
    case TRIP_SW1_CUR_UP:
        if(ServiceResetSetNum == 1)
        {
            ServiceResetSetNum = 0;
            USMSelectMovePoint = 32;
        }
        else
        {
            ServiceResetSetNum = 1;
        }
        break;
    case TRIP_SW1_CUR_OK:
        if(ServiceResetSetNum == 0)
        {
            Flag_ServiceReset = 0;
            ServiceResetSetNum = 0;
            ServicePara.RemainKm = ServicePara.SetSaveKm;
            ServicePara.RemainDay = ServicePara.SetSaveMonth * 365 / 12;
            ServicePara.RemainSeconds = (int32_t)ServicePara.RemainDay * 24 * 3600;
            SaveServiceRemainderValid = 1;
        }
        else
        {
            Flag_ServiceReset = 0;
            ServiceResetSetNum = 0;
        }
    }
}
