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

namespace ChamChat.Models
{
    public class Loop
    {
        /// <summary>
        /// Loop
        /// </summary>
        /// <param name="N">Number of times to perform the steps in the loop</param>
        /// <param name="First">First step in loop (index in Profile.Steps)</param>
        /// <param name="Last">Last step in loop (index in Profile.Steps)</param>
        public Loop(Int32 N, Int32 First, Int32 Last)
        {
            _N = N;
            _First = First;
            _Last = Last;
        }

        private Int32 _N = 0; public Int32 N { get { return _N; } set { _N = value; } }
        private Int32 _First = 0; public Int32 First { get { return _First; } set { _First = value; } }
        private Int32 _Last = 0; public Int32 Last { get { return _Last; } set { _Last = value; } }


        public Boolean Equals(Loop l)
        {
            return l.ToString() == this.ToString();
        }


        public override string ToString()
        {
            String s = string.Format("{2}x[s{0}..s{1}]", _First, _Last, _N);
            return s;
        }


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

            try
            {
                _N = Int32.Parse(XML.GetTags("_N"));
                _First = Int32.Parse(XML.GetTags("_First"));
                _Last = Int32.Parse(XML.GetTags("_Last"));

            }
            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("<_N>{0:0}</_N>", _N);
                xml += String.Format("<_First>{0:0}</_First>", _First);
                xml += String.Format("<_Last>{0:0}</_Last>", _Last);
                xml += "</" + this.GetType().Namespace + "." + this.GetType().Name + ">";
                return  xml;
            }
        }

    }





    public static class ExtensionsToLoop
    {
        public static List<Loop> OrderByLastAsc(this List<Loop> list)
        {
            List<Loop> result = new List<Loop>();
            List<Loop> copy = new List<Loop>();

            
            if (list == null)
                return result;

            if (list.Count < 2)
                return list;

            foreach (Loop l in list)
                copy.Add(l);


            while (copy.Count > 0)
            {
                Loop lowest = copy[0];
                foreach (Loop l in copy)
                {
                    if (l.Last == lowest.Last)
                        if (l.First > lowest.First)
                            lowest = l;

                    if (l.Last < lowest.Last)
                        lowest = l;
                }
                result.Add(lowest);
                copy.Remove(lowest);
            }
            return result;
        }
    }

}
