using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TZONE.Model;
using TZONE.GPSSocketServer.Common;

namespace TZONE.GT08.DD
{
    public class AnalyzeHandle
    {
        /// <summary>
        /// Data Analysis
        /// </summary>
        /// <param name="temp"></param>
        public void AnalyzeData(tempdata temp)
        {
            normal model = new normal();
            model.ServerTime = Convert.ToDateTime(temp.server_time);
            model.Message = temp.message;
            model.IMEI = temp.imei;
            model.PID = temp.PID;
            model.TypeID = temp.type_id;
            model.TrackerTime = Convert.ToDateTime("1900-01-01");
            model.Alram = "AA";
            model.GPRMCState = "V";
            model.Lat = 0;
            model.NS = "N";
            model.Lng = 0;
            model.WE = "E";
            model.Speed = 0;
            model.Direction = 0;
            if (ProtocolAnalyze(ref model))
            {
                model.IsError = 0;
                model.Address = "";
                model.C = "";
                DataAccess.data_Facade dal = new DataAccess.data_Facade();
                dal.insert_data(model);
            }
        }

        /// <summary>
        /// Protocol analysis
        /// </summary>
        /// <param name="model"></param>
        private bool ProtocolAnalyze(ref normal model)
        {
            try
            {
                /*
                 * Format：
                 * 
                 * $$(2 Bytes) + Len(2 Bytes) + IMEI(15 Bytes) + | + AlarmType(2 Bytes) + GPRMC + | 
                 * + PDOP + | + HDOP + | + VDOP + | + Status(12 Bytes) + | + RTC(14 Bytes) + | + Voltage(8 Bytes) + | 
                 * + LACCI(8 Bytes) + | + Temperature(4 Bytes) | + Mile-meter(14 Bytes)+ |  Serial(4 Bytes) + | 
                 * + Checksum (4 Byte) + \r\n(2 Bytes)
                 * 
                 */
                string[] _temp = model.Message.Split(new char[] { '|' });
                if (_temp.Count() >= 12)
                {
                    model.Length = Convert.ToInt32(_temp[0].Substring(2, 2), 16);
                    string[] _gprmc = _temp[1].Split(new char[] { ',' });
                    model.Alram = _gprmc[0].Substring(0, 2).ToString();
                    if (_gprmc.Count() > 10)
                    {
                        model.GPRMCState = _gprmc[2];
                        model.Lat = GPSSocketServer.Common.Utils.GetLat(_gprmc[3], _gprmc[4]);
                        model.NS = _gprmc[4];
                        model.Lng = GPSSocketServer.Common.Utils.GetLng(_gprmc[5], _gprmc[6]);
                        model.WE = _gprmc[6];
                        model.Speed = GPSSocketServer.Common.Utils.GetDecimal(_gprmc[7]);
                        model.Direction = GPSSocketServer.Common.Utils.GetDecimal(_gprmc[8]);
                        model.TrackerTime = GPSSocketServer.Common.Utils.stringTodatetime(_gprmc[9], _gprmc[1]);
                    }
                    model.PDOP = GPSSocketServer.Common.Utils.GetDecimal(_temp[2]);
                    model.HDOP = GPSSocketServer.Common.Utils.GetDecimal(_temp[3]);
                    model.VDOP = GPSSocketServer.Common.Utils.GetDecimal(_temp[4]);
                    #region IO
                    model.Status = _temp[5];
                    #endregion
                    model.RTC = DateTime.ParseExact(_temp[6], "yyyyMMddHHmmss", null);
                    model.VCS = _temp[7].Substring(0, 1);
                    model.VBV = GPSSocketServer.Common.Utils.GetDecimal(_temp[7].Substring(1, 3));
                    model.VCV = GPSSocketServer.Common.Utils.GetDecimal(_temp[7].Substring(4));
                    model.ADA = 0;
                    model.ADB = 0;
                    model.LACCI = _temp[8];
                    model.Temperature = GPSSocketServer.Common.Utils.GetDecimal(_temp[9]);
                    model.Mileage = GPSSocketServer.Common.Utils.GetDecimal(_temp[10]);
                    model.Serial = _temp[11];
                    model.RFID = "";
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
