using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChamChat
{
    public partial class TSx_Client : Client
    {
        public bool ShiftToA()
        {
            string sAddr = _Address.ToString();
            string cmd;
            string read;

            cmd = sAddr + ", TAREA?";
            DataTransfer(cmd, out read, _ReturnDataWaitms);

            // If moving, wait to finish
            if (read.Contains("MOVE"))
            {
                bool moving = true;
                while (moving)
                {
                    System.Threading.Thread.Sleep(2000);
                    DataTransfer(cmd, out read, _ReturnDataWaitms);
                    if (!read.Contains("MOVE"))
                        moving = false;
                }
            }

            // Already in Cold 
            if (read.Contains("C"))
                return true;


            cmd = sAddr + ", MODE, TAREAMOVE";
            DataTransfer(cmd, out read, _ReturnDataWaitms);

            System.Threading.Thread.Sleep(5000); // Wait for move to finish

            cmd = sAddr + ", TAREA?";
            DataTransfer(cmd, out read, _ReturnDataWaitms);

            // If moving, wait to finish
            if (read.Contains("MOVE"))
            {
                bool moving = true;
                while (moving)
                {
                    System.Threading.Thread.Sleep(2000);
                    DataTransfer(cmd, out read, _ReturnDataWaitms);
                    if (!read.Contains("MOVE"))
                        moving = false;
                }
            }

            // Already in Cold 
            if (read.Contains("C"))
                return true;

            // Not in Cold, not moving towards cold
            return false;
        }

        public bool ShiftToB()
        {
            string sAddr = _Address.ToString();
            string cmd;
            string read;

            cmd = sAddr + ", TAREA?";
            DataTransfer(cmd, out read, _ReturnDataWaitms);

            // If moving, wait to finish
            if (read.Contains("MOVE"))
            {
                bool moving = true;
                while (moving)
                {
                    System.Threading.Thread.Sleep(2000);
                    DataTransfer(cmd, out read, _ReturnDataWaitms);
                    if (!read.Contains("MOVE"))
                        moving = false;
                }
            }

            // Already in Hot 
            if (read.Contains("H"))
                return true;


            cmd = sAddr + ", MODE, TAREAMOVE";
            DataTransfer(cmd, out read, _ReturnDataWaitms);

            System.Threading.Thread.Sleep(5000); // Wait for move to finish

            cmd = sAddr + ", TAREA?";
            DataTransfer(cmd, out read, _ReturnDataWaitms);

            // If moving, wait to finish
            if (read.Contains("MOVE"))
            {
                bool moving = true;
                while (moving)
                {
                    System.Threading.Thread.Sleep(2000);
                    DataTransfer(cmd, out read, _ReturnDataWaitms);
                    if (!read.Contains("MOVE"))
                        moving = false;
                }
            }

            // In Hot
            if (read.Contains("H"))
                return true;

            // Not in Hot, not moving towards Hot
            return false;
        }

        public Boolean AwaitMoving(Int32 timeOutSec)
        {
            string sAddr = _Address.ToString();
            string cmd = sAddr + ", TAREA?";;
            string read;

            DateTime dtTimeOut = DateTime.Now.AddSeconds(timeOutSec);
            while (DateTime.Now < dtTimeOut)
            {
                DataTransfer(cmd, out read, _ReturnDataWaitms);
                if (!read.Contains("MOVE"))
                    return true;
                System.Threading.Thread.Sleep(2000);
            }
            
            return false;
        }

        public Double CurrentTemperature()
        {
            string sAddr = _Address.ToString();
            string cmd;
            string read;
            
            cmd = sAddr + ", TEMP?";  // See page 36
            DataTransfer(cmd, out read, _ReturnDataWaitms,1);
            return Double.Parse(read.Split(',')[6]);
        }
    }
}
