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 GPIOControlImpl : IoControllService
    {
        public Boolean yellowFlash = false;

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

        public void initIO() {
            GPIO.Initialize();
        }

        public int getSensorVal()
        {
            int re = -1;
            try
            {
                re = GPIO.getSensorValue();
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
            return re;
        }

        public void RedOn()
        {
            try
            {
                GPIO.redOn();
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void RedOff()
        {
            try
            {
                GPIO.redOff();
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void GreenOn()
        {
            try
            {
                GPIO.greenOn();
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void GreenOff()
        {
            try
            {
                GPIO.greenOff();
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

        public void YellowOn()
        {
            try
            {
                GPIO.yellowOn();
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }

        }

        public void YellowOff()
        {
            try
            {
                yellowFlash = false;
                GPIO.yellowOff();
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, e.Message.ToString());
            }
        }

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

                    if (i % 2 == 0) GPIO.yellowOn();
                    else GPIO.yellowOff();

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


    }
}
