using irisHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

namespace irisIoControll.Service.Impl
{
    public class SusiControlImpl : IoControllService
    {
        public Boolean yellowFlash = false;

        public void setYellowFlash(bool bl)
        {
            yellowFlash = bl;
        }

        public void initIO()
        {
            susiControl.SusiInitializer();

            susiControl.setBacklightEnable(0, 0);
        }

        public int getSensorVal()
        {
            int re = -1;
            try
            {
                re = (int)susiControl.GetLevel(6);

            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
            return re;
        }

        public void RedOn()
        {
            try
            {

                susiControl.SetLevel(1, 2);

            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void RedOff()
        {
            try
            {

                susiControl.SetLevel(0, 2);
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void GreenOn()
        {
            try
            {
                susiControl.SetLevel(1, 3);
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void GreenOff()
        {
            try
            {
                susiControl.SetLevel(0, 3);

            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void YellowOn()
        {
            try
            {
                susiControl.SetLevel(1, 7);
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }

        }

        public void YellowOff()
        {
            try
            {
                yellowFlash = false;


                susiControl.SetLevel(0, 7);

            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void YellowFlash()
        {
            try
            {
                int i = 0;
                while (yellowFlash)
                {

                    if (i % 2 == 0) susiControl.SetLevel(1, 7);
                    else susiControl.SetLevel(0, 7);

                    i++;
                    Thread.Sleep(100);
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

    }

}