using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Data.Sqlite;
using System.Reflection;
using System.Windows.Forms;

namespace DataOrganizer
{
    public class DB
    {
        public static string DatabasePath { get; private set; }
        public static string ConnectionString { get; private set; }
        public static string TempScriptLocal = "Y:\\Database Template.sql";

        public DB()
        {
            DatabasePath = $"{new FileInfo(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath).Directory.FullName}\\Organizer Data\\Main.sqlite";

            ConnectionString = $"Data Source={DatabasePath}";

            if (!Directory.Exists(new FileInfo(DatabasePath).Directory.FullName))
            {
                Directory.CreateDirectory(new FileInfo(DatabasePath).Directory.FullName);
                CreateDatabase();
            }
        }

        private void CreateDatabase()
        {
            //TODO: this is tmp
            var query = File.ReadAllText(TempScriptLocal);

            using (var connection = new SqliteConnection(ConnectionString))
            {
                using(var command = new SqliteCommand(query, connection))
                {
                    connection.Open();

                    command.ExecuteNonQuery();
                }
            }
        }

        public List<Profile> GetProfiles()
        {
            var query = "SELECT * FROM profile";
            var profiles = new List<Profile>();

            using (var connection = new SqliteConnection(ConnectionString))
            {
                using (var command = new SqliteCommand(query, connection))
                {
                    connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        profiles.Add(new Profile
                        {
                            Name = reader["profile_name"].ToString(),
                            DefaultNewContentFolder = reader["default_new_content_folder_name"].ToString(),
                            MachineName = reader["machine_name"].ToString(),
                            RowId = Convert.ToInt32(reader["profile_id"])
                        });
                    }
                }
            }

            return profiles;
        }

        public bool UpdateProfile(Profile profile)
        {
            var query = @"UPDATE main.profile SET machine_name = @MachineName, default_new_content_folder_name = @DefaultPath, profile_name = @Name WHERE profile_id = @ID";
            using (var connection = new SqliteConnection(ConnectionString))
            {
                using (var command = new SqliteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("MachineName", profile.MachineName);
                    command.Parameters.AddWithValue("DefaultPath", profile.DefaultNewContentFolder);
                    command.Parameters.AddWithValue("Name", profile.Name);
                    command.Parameters.AddWithValue("ID", profile.RowId);

                    connection.Open();

                    command.ExecuteNonQuery();
                }
            }

            return true;
        }

        public bool InsertNewProfile(Profile profile)
        {
            var query = @"INSERT INTO main.profile (machine_name, default_new_content_folder_name, profile_name) VALUES (@MachineName, @DefaultPath, @Name)";
            using (var connection = new SqliteConnection(ConnectionString))
            {
                using (var command = new SqliteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("MachineName", profile.MachineName);
                    command.Parameters.AddWithValue("DefaultPath", profile.DefaultNewContentFolder);
                    command.Parameters.AddWithValue("Name", profile.Name);

                    connection.Open();

                    command.ExecuteNonQuery();
                }
            }

            return true;
        }

        public struct Profile
        {
            public string Name;
            public string DefaultNewContentFolder;
            public string MachineName;
            public int RowId;
        }
    }
}
