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

namespace ChamChat.Models
{
    public class Step
    {
        /// <summary>
        /// Create a new profile step
        /// </summary>
        /// <param name="Duration">Duration of step in minutes</param>
        public Step(Double Duration)
        {
            _DurationMin = Duration;
        }

      



        private double _DurationMin = 0;
        private int _PK = 0; // PK is index in Step list
        private double _RH = 0; // RH = 0 means RH OFF
        private double _T = 0;
        private bool _RampCtrlRH = false;
        private bool _RampCtrlT = false;
        private double _PreTemp = 0;
        private double _LimitT = 0;

        public Double DurationMin { get { return _DurationMin; } set { _DurationMin = value; } }
        public Int32 PK { get { return _PK; } set { _PK = value; } }    
        public Double RH { get { return _RH; } set { _RH = value; } }
        public Double T { get { return _T; } set { _T = value; } }
        public Boolean RampCtrlRH { get { return _RampCtrlRH; } set { _RampCtrlRH = value; } }
        public Boolean RampCtrlT { get { return _RampCtrlT; } set { _RampCtrlT = value; } }
        public Double PreTemp { get { return _PreTemp; }            set { _PreTemp = value; }         }
        public Double LimitT { get { return _LimitT; } set { _LimitT = value; } }

        public Int32 Hours { get { return (int)Math.Floor(_DurationMin / 60); } }
        public Int32 Minutes { get { return (int)Math.Round(_DurationMin - 60 * Hours); } }

        public  string ToFormattedString()
        {
            string r ="";

            if (_DurationMin > 60)
                r = String.Format("{0:0} hours and {1:00} minutes ", Hours, Minutes);
            else
                r = String.Format("{0:0} minutes ", _DurationMin);

            r += "\n";

            if (_RampCtrlT)
                r += String.Format("{0:0.0} °C (R-ctrl ON) ", _T);
            else
                r += String.Format("{0:0.0} °C (R-ctrl OFF) ", _T);


            if (_RH > 0)
            {
                if (_RampCtrlRH)
                    r += String.Format("\n{0:0.0} %rh (R-ctrl ON)", _RH);
                else
                    r += String.Format("\n{0:0.0} %rh (R-ctrl OFF)", _RH);
            }

            if (_PreTemp < double.MaxValue - 1 && !_RampCtrlT)
                r += String.Format("\npre-T {0:0.0} °C, ", _PreTemp);

            return r;
        }

        public override string ToString()
        {
            string r = String.Format("PK={0:00}: {1:0} min, ",_PK, _DurationMin);

            if (_DurationMin >= 60)
                r = String.Format("{0:0} hours & {1:00} minutes  ", Hours,Minutes);
            else
                r = String.Format("{0:0} minutes  ", _DurationMin);

            r += "\n";

            if (_RampCtrlT)
                r += String.Format(" {0:0.0} °C (R-ctrl ON) ", _T);
            else
                r += String.Format(" {0:0.0} °C (R-ctrl OFF) ", _T);


            if (_RH > 0)
            {
                if (_RampCtrlRH)
                    r += String.Format("\n{0:0.0} %rh (R-ctrl ON)", _RH);
                else
                    r += String.Format("\n{0:0.0} %rh (R-ctrl OFF)", _RH);
            }

            if(!double.IsNaN(_PreTemp))
                r += String.Format("\npre-T {0:0.0} °C, ", _PreTemp);
            
            return r;
        }

        public Boolean HasRH
        {
            get
            {
                if (RH > 0)
                    return true;
                return false;
            }
        }


        public Step(String XML)
        {
            string fp = "<" + this.GetType().Namespace + "." + this.GetType().Name + ">";
            if (!XML.Contains(fp))
                throw new Exception(fp + " tag not found!");

            try
            {
                _DurationMin = Double.Parse(XML.GetTags("_DurationMin"));
                _PK = Int32.Parse(XML.GetTags("_PK"));
                _RH = Double.Parse(XML.GetTags("_RH"));
                _T = Double.Parse(XML.GetTags("_T"));
                _RampCtrlRH = Boolean.Parse(XML.GetTags("_RampCtrlRH"));
                _RampCtrlT = Boolean.Parse(XML.GetTags("_RampCtrlT"));
                _PreTemp = Double.Parse(XML.GetTags("_PreTemp"));
                _LimitT = Double.Parse(XML.GetTags("_LimitT"));
            }
            catch
            {
                throw new Exception(fp +": could not create instance from input string!");
            }


        }

        public String XML
        {
            get
            {
                string xml = "";
                xml += "<"+this.GetType().Namespace + "." + this.GetType().Name + ">";
                xml += String.Format("<_DurationMin>{0:0.0}</_DurationMin>", _DurationMin);
                xml += String.Format("<_PK>{0:0}</_PK>", _PK);
                xml += String.Format("<_RH>{0:0.00}</_RH>", _RH);
                xml += String.Format("<_T>{0:0.00}</_T>", _T);
                xml += String.Format("<_RampCtrlRH>{0}</_RampCtrlRH>", _RampCtrlRH);
                xml += String.Format("<_RampCtrlT>{0}</_RampCtrlT>", _RampCtrlT);
                xml += String.Format("<_PreTemp>{0:0.00}</_PreTemp>", _PreTemp);
                xml += String.Format("<_LimitT>{0:0.00}</_LimitT>", _LimitT);
                xml += "</" + this.GetType().Namespace + "." + this.GetType().Name + ">";
                return  xml;
            }
        }
    }



    public static class StepExtensionMethods
    {
        public static Step DeepClone(this Step s)
        {
            Step r = new Step(s.DurationMin);
            r.RH = s.RH;
            r.T = s.T;
            r.RampCtrlRH = s.RampCtrlRH;
            r.RampCtrlT = s.RampCtrlT;
            r.PreTemp = s.PreTemp;
            r.LimitT = s.LimitT;
            r.PK = s.PK;
            return r;
        }
    }



}
