using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FLY.Thick.RemoteHistory;
namespace FLY.Thick.Base.Common
{
    public class FlyData_ScanHistory:IFlyData
    {
        public DateTime Time { get; set; }
        public int[] SampleAD=new int[3];
        public double Comp;
        public int Shift;
        public int Target;
        public int Alarm;
        public bool Valid;
        public int BoltCnt;
        public int BoltNo1st;
        public int[] Data;
        public int Avg;//平均值

        #region IFlyData 成员

        public string GetHeader()
        {
            string header = "时间,温修AD,样品1AD,样品2AD,斜率补偿,平移补偿,目标值,公差,有效";
            for (int i = 0; i < BoltCnt; i++)
            {
                header += "," + (BoltNo1st + i).ToString();
            }
            header += ",平均值";

            return header;
        }
        public override string ToString()
        {
            string str;
            str = Time.ToString() ;
            for (int i = 0; i < 3; i++)
                str += "," + SampleAD[i].ToString();
            str += "," + Comp.ToString("F2");
            str += "," + (Shift/100.0).ToString("F2");
            str += "," + (Target/100.0).ToString("F2");
            str += "," + (Alarm/100.0).ToString("F2");
            str += "," + Valid.ToString();
            int sum = 0;
            int cnt = 0;
            for (int i = 0; i < BoltCnt && i<Data.Count(); i++)
            {
                
                int thick = Data[i];
                str += ",";

                if (Misc.MyBase.ISVALIDATA(thick))
                {
                    str += (thick / 100.0).ToString("F2");
                    sum+=thick;
                    cnt++;
                }
            }
            str += ",";
            if (cnt > 0)
                str += (sum / cnt / 100.0).ToString("F2");

            return str;
        }
        public bool TryParse(string header, string str)
        {
            string[] items = header.Split(new char[] { ',' });
            if (items.Length < 9)
                return false;

            int boltno1st;

            if (int.TryParse(items[9], out boltno1st))
                BoltNo1st = boltno1st;
            else
                return false;

            BoltCnt = items.Length - 9 - 1;

            items = str.Split(new char[] { ',' });
            if ((items.Length-9-1) !=BoltCnt)
                return false;

            DateTime dt;
            int _i;
            double d;
            bool b;

            if (DateTime.TryParse(items[0], out dt))
                Time = dt;
            else
                return false;

            for (int i = 0; i < 3; i++) 
            {

                if (int.TryParse(items[1 + i], out _i))
                    SampleAD[i] = _i;
                else
                    return false;
            }
            if (double.TryParse(items[4], out d))
                Comp = d;
            else
                return false;


            if (double.TryParse(items[5], out d))
                Shift = (int)d*100;
            else
                return false;

            if (double.TryParse(items[6], out d))
                Target = (int)d * 100;
            else
                return false;

            if (double.TryParse(items[7], out d))
                Alarm = (int)d * 100;
            else
                return false;

            if (bool.TryParse(items[8], out b))
                Valid = b;
            else
                return false;

            Data = new int[BoltCnt];

            for (int i = 0; i < BoltCnt; i++)
            {
                string s = items[9 + i];
                if (string.IsNullOrEmpty(s))
                {
                    Data[i] = Misc.MyBase.NULL_VALUE;
                }
                else
                {
                    if (double.TryParse(s, out d))
                        Data[i] = (int)(d * 100);
                    else
                        return false;
                }
            }
            {
                string s = items[9 + BoltCnt];
                if (string.IsNullOrEmpty(s))
                {
                    Avg = Misc.MyBase.NULL_VALUE;
                }
                else
                {
                    if (double.TryParse(s, out d))
                        Avg = (int)d * 100;
                    else
                        return false;
                }
            }
            

            return true;
        }

        #endregion
    }
}
