using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Spice.Models
{
    public class db
    {
        public db()
        {

        }

        public Task<List<object[]>> GetDbData(ref Object[] array, string cs, string magnitude, string deveui)
        {
            Object[] myarray = new Object[array.Length];
            List<object[]> list = new List<object[]>();
            return Task.Run(() =>
            {
                int i = 0;
                string db_str = "Select dtime, " + magnitude + " from iot_devices where eui = '" + deveui + "' and dtime > '2020-03-21 00:00:00' ";
                using (MySqlConnection con = new MySqlConnection(cs))
                {
                    MySqlCommand cmd = new MySqlCommand(db_str, con);
                    con.Open();
                    MySqlDataReader rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        string htemp = rdr[magnitude + ""].ToString();
                        string ddtime = Convert.ToDateTime(rdr["dtime"]).ToString("dd-MM-yyyy HH:mm:ss");
                        DateTime dtime = Convert.ToDateTime(rdr["dtime"]);//.ToString("dd-MM-yyyy HH:mm:ss"));
                        var datetime = new DateTime(dtime.Ticks, DateTimeKind.Utc);
                        
                        var datetimeoffset = new DateTimeOffset(datetime);
                        var unix = datetimeoffset.ToUnixTimeMilliseconds();
                        
                        list.Add(new object[] { unix, (int.Parse(htemp, System.Globalization.NumberStyles.HexNumber)) / 100.0 });
                        i++;
                    }
                }
                
                return list;
            });
            
        }

        public Task<List<object[]>> GetDbData(ref Object[] array, string cs, long min, long max, string magnitude, string deveui)
        {
            Object[] myarray = new Object[array.Length];
            List<object[]> list = new List<object[]>();
            return Task.Run(() =>
            {
                long i = 0;
                var datetimemin = DateTimeOffset.FromUnixTimeMilliseconds(min).ToString("yyyy-MM-dd HH:mm:ss");
                var datetimemax = DateTimeOffset.FromUnixTimeMilliseconds(max).ToString("yyyy-MM-dd HH:mm:ss");
                
                string db_str1 = "Select dtime, " + magnitude + " from iot_devices where eui = '" + deveui + "' and dtime between '" + datetimemin + "' and '" + datetimemax + "' order by dtime";
                using (MySqlConnection con = new MySqlConnection(cs))
                {
                    MySqlCommand cmd = new MySqlCommand(db_str1, con);
                    con.Open();
                    MySqlDataReader rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        string htemp = rdr[magnitude + ""].ToString();
                        string ddtime = Convert.ToDateTime(rdr["dtime"]).ToString("dd-MM-yyyy HH:mm:ss");
                        DateTime dtime = Convert.ToDateTime(rdr["dtime"]);//.ToString("dd-MM-yyyy HH:mm:ss"));
                        var datetime = new DateTime(dtime.Ticks, DateTimeKind.Utc);

                        var datetimeoffset = new DateTimeOffset(datetime);
                        var unix = datetimeoffset.ToUnixTimeMilliseconds();
                       
                        list.Add(new object[] { unix, (int.Parse(htemp, System.Globalization.NumberStyles.HexNumber)) / 100.0 });
                        i++;
                    }
                }

                return list;
            });

        }


    }
}
