
using Teltonika.Logging;
using System.Data;
namespace FM21XXConfigurator.Common {
    
    
    public partial class Profile {
        public readonly static double AllowedFloatError = 0.0001f;

        partial class SmsWeekDayDataTable
        {
        }
    
        partial class ServerNumDataTable
        {
        }

        public Profile(string version)
        {
        }

        /// <summary>
        /// True if dataset has all required data.
        /// </summary>
        public bool HasData
        {
            get
            {
                bool hasData =
                    this._Profile.Count == 1 &&
                    this.Sys.Count == 1 &&
                    this.Gps.Count == 1 &&
                    this.Geofence.Count == 1 &&
                    this.Frame.Count == Constants.ProfileGeoFencesCount &&
                    this.Gsm.Count == 1 &&
                    this.Gprs.Count == 1 &&
                    this.Sms.Count == 1 &&
                    this.Securenum.Count == 9 &&
                    this.Operator.Count == 1 &&
                    this.OperatorCode.Count == Constants.ProfileOperatorsCount &&
                    this.SmsWeekDay.Count == 1 &&
                    this.SmsDayTime.Count == 18 &&
                    this.SendWeekDay.Count == 1 &&
                    this.SendDayTime.Count == 18 &&
                    this.IO.Count == Constants.ProfileIOCount;

                return hasData;
            }
        }

        public short CurrentProfileId
        {
            get
            {
                return this._Profile[0].ProfileId;
            }
        }

        public short CurrentGeofenceId
        {
            get { return 0; }
        }

        public short CurrentOperatorId
        {
            get { return 0; }
        }

        public short CurrentServerNumId
        {
            get
            {
                return 0;
            }
        }

        public short NewFrameId
        {
            get
            {
                short nextFrameId = 1; //0
                System.Data.DataRow[] frameRows = this.Frame.Select(string.Empty, "FrameId DESC");
                if (frameRows.Length > 0)
                {
                    nextFrameId = (short)frameRows[0]["FrameId"];
                    nextFrameId++;
                }
                return nextFrameId;
            }
        }

        public short NewIOId
        {
            get
            {
                short newIOId = 0;
                System.Data.DataRow[] ioRows = this.IO.Select(string.Empty, "IOId DESC");
                if (ioRows.Length > 0)
                {
                    newIOId = (short)ioRows[0]["IOId"];
                    newIOId++;
                }
                return newIOId;
            }
        }

        public short NewOperatorCodeId
        {
            get
            {
                short newOpCodeId = 0;
                System.Data.DataRow[] opCodeRows = this.OperatorCode.Select(string.Empty, "OperatorCodeId DESC");
                if (opCodeRows.Length > 0)
                {
                    newOpCodeId = (short)opCodeRows[0]["OperatorCodeId"];
                    newOpCodeId++;
                }
                return newOpCodeId;
            }
        }

        //public Profile Copy()
        //{
        //    Profile newProfile = new Profile();
        //    foreach(DataTable table in Tables)
        //    {
                
        //    }

        //    return newProfile;
        //}


        public override bool Equals(object obj)
        {
            // If parameter cannot be cast to ThreeDPoint return false:
            Profile p = obj as Profile;
            if ((object)p == null)
            {
                return false;
            }

            bool rezult = true;

            foreach (DataTable table in Tables)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    for (int index = 0; index < table.Columns.Count; index++)
                    {
                        if (table.Rows[i][index] is double && System.Math.Abs((double)table.Rows[i][index] - (double)p.Tables[table.TableName].Rows[i][index]) > AllowedFloatError
                            || !(table.Rows[i][index] is double) && !table.Rows[i][index].Equals(p.Tables[table.TableName].Rows[i][index]))
                        {
                            if (table.Columns[index].ColumnName != "ProfileId")
                            {
                                Log(string.Format("Parameter haven't written: Table {0}, Parameter{1}", table.TableName, table.Columns[index].ColumnName));
                                rezult = false;
                            }
                        }
                    }
                }
            }
            
            return rezult;
        }

        public bool Equals_ShowsError(object obj , out string Error) //GV
        {
            Error = "";
            // If parameter cannot be cast to ThreeDPoint return false:
            Profile p = obj as Profile;
            if ((object)p == null)
            {
                return false;
            }

            bool rezult = true;

            foreach (DataTable table in Tables)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    for (int index = 0; index < table.Columns.Count; index++)
                    {
                        if (table.Rows[i][index] is double && System.Math.Abs((double)table.Rows[i][index] - (double)p.Tables[table.TableName].Rows[i][index]) > AllowedFloatError
                            || !(table.Rows[i][index] is double) && !table.Rows[i][index].Equals(p.Tables[table.TableName].Rows[i][index]))
                        {
                            if (table.Columns[index].ColumnName != "ProfileId")
                            {
                                Log(string.Format("Parameter haven't written: Table {0}, Parameter{1}", table.TableName, table.Columns[index].ColumnName));
                                Error += string.Format("Table {0}, Parameter{1} \n", table.TableName, table.Columns[index].ColumnName); //GV
                                rezult = false;
                            }
                        }
                    }
                }
            }

            return rezult;
        }

        private static void Log(string msg)
        {
            ILog log = LogManager.GetLogger("Profile");
            if (log.IsInfoEnabled)
            {
                log.Info(msg);
            }
        }


        //
        // 
        //
        //private Profile _backup = null;
        //public void BackUp()
        //{
        //    if (this.HasData)
        //    {
        //        _backup = (Profile)this.Copy();
        //    }
        //}

        //public void Restore()
        //{
        //    if (_backup != null)
        //    {
        //    }
        //}
    }
}
