//#define OLD

using Project;
using Project.Device;
using System;

#if OLD
using static Project.StateMachine;
#endif

/// <summary>
/// 타워램프조작
/// </summary>
public static class TowerLamp
{
    static bool enable = true;
#if OLD
    static arDev.AzinAxt.DIO DIO = null;
#else
    static arDev.DIO.IDIO DIO = null;
#endif
    static int R = -1;
    static int G = -1;
    static int Y = -1;
    static DateTime updatetime = DateTime.Now;

    /// <summary>
    /// 타워램프 사용여부 false 가 입력되면 모든 램프가 꺼집니다
    /// </summary>
    public static Boolean Enable
    {
        get { return enable; }
        set
        {
            enable = value;
            if (enable == false)
            {
                RED = false;
                YEL = false;
                GRN = false;
            }
        }
    }

    /// <summary>
    /// 갱신주기 250ms 기본설정
    /// </summary>
    public static int UpdateInterval = 250;

#if OLD

    public static void Init(arDev.AzinAxt.DIO dio_, int R_, int G_, int Y_)
#else
    public static void Init(arDev.DIO.IDIO dio_, int R_, int G_, int Y_)
#endif


    {
        DIO = dio_;
        R = R_;
        G = G_;
        Y = Y_;
    }
    public static void Update(eSMStep Step)
    {
        if (Enable == false) return;
        if (DIO == null) return; ;// throw new Exception("DIO가 설정되지 않았습니다. Init 함수를 호출 하세요");

        var ts = DateTime.Now - updatetime;
        if (ts.TotalMilliseconds < UpdateInterval) return;

        if (Step == eSMStep.RUN)  //동작중에는 녹색을 점등
        {
            if (GRN == false) GRN = true;
            if (RED == true) RED = false;
            if (YEL == true) YEL = false;
        }
        else if (Step < eSMStep.IDLE ||  Step.ToString().StartsWith("HOME"))    //초기화관련
        {
            GRN = false;
            var cur = YEL;
            RED = !cur;
            YEL = !cur;
        }
        else if (Step == eSMStep.FINISH) //작업종료
        {
            var cur = GRN;
            GRN = !cur;
            YEL = !cur;
            RED = false;
        }
        else if (Step == eSMStep.WAITSTART) //사용자대기
        {
            var cur = YEL;
            YEL = !cur;
            GRN = false;
            RED = false;
        }
        else if (Step == eSMStep.ERROR || Step == eSMStep.EMERGENCY)
        {
            RED = !RED;
            YEL = false;
            GRN = false;
        }
        else if (Step == eSMStep.PAUSE)
        {
            if (RED == false) RED = true;
            if (YEL == true) YEL = false;
            if (GRN == true) GRN = false;
        }
        else
        {
            //나머지 모든 상황은 대기로 한다
            if (YEL == false) YEL = true;
            if (GRN == true) GRN = false;
            if (RED == true) RED = false;
        }
    }


    static void SetLamp(int port, bool value)
    {
        if (DIO == null || !DIO.IsInit || port < 0) return;
        DIO.SetOutput(port, value);
    }
    static bool GetLamp(int port)
    {
        if (DIO == null || !DIO.IsInit || port < 0) return false;
#if OLD
        return DIO.OUTPUT(port);
#else
        return DIO.GetDOValue(port);
#endif
    }

    public static Boolean GRN
    {
        get { return GetLamp(G); }
        set { SetLamp(G, value); }
    }
    public static Boolean YEL
    {
        get { return GetLamp(Y); }
        set { SetLamp(Y, value); }
    }
    public static Boolean RED
    {
        get { return GetLamp(R); }
        set { SetLamp(R, value); }
    }
}

//230619    chi PAUSE 상태추가 => RED ON
//              전처리추가                