using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace DBBackupTool.Helpers;

internal class PostgreSQLTool
{
    //HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql*
    private string? pg_dump;
    private string? pg_restore;

    public bool Initialize()
    {
        RegistryKey? key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\PostgreSQL\\Installations");
        key ??= Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\PostgreSQL\\Installations");

        if (key is null)
            return false;

        if (key.GetSubKeyNames().FirstOrDefault(t => t.Contains("postgresql")) is not string nextKey)
            return false;

        key = key.OpenSubKey(nextKey);
        if (key?.GetValue("Base Directory") is not string path)
            return false;

        this.pg_dump = Path.Combine(path, "bin", "pg_dump.exe");
        this.pg_restore = Path.Combine(path, "bin", "pg_restore.exe");

        return true;
    }

    public bool BackUp(string serverAddress, int port, string userName, string password, string savePath, string dbName)
    {
        if (string.IsNullOrEmpty(this.pg_dump))
            return false;

        Process process = new();
        process.StartInfo.Environment.Add("PGPASSWORD", password);
        process.StartInfo.FileName = this.pg_dump;
        process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" {dbName}");
        process.StartInfo.CreateNoWindow = true;
        process.Start();
        process.WaitForExit();
        return true;
    }



    public bool Restore(string serverAddress, int port, string userName, string password, string savePath, string dbName)
    {
        if (string.IsNullOrEmpty(this.pg_restore))
            return false;

        Process process = new();
        process.StartInfo.Environment.Add("PGPASSWORD", password);
        process.StartInfo.FileName = this.pg_restore;
        process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -v -d {dbName} --clean \"{savePath}\"");
        process.StartInfo.CreateNoWindow = true;
        process.Start();
        process.WaitForExit();

        return true;
    }

    public bool BackUpSingleTable(string serverAddress, int port, string userName, string password, string savePath, string dbName, string tableName)
    {
        if (string.IsNullOrEmpty(this.pg_dump))
            return false;

        Process process = new Process();
        process.StartInfo.RedirectStandardOutput = true;

        process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
        process.StartInfo.Environment.Add("PGPASSWORD", password);
        process.StartInfo.FileName = this.pg_dump;
        process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table \"{tableName}\" {dbName}");
        process.StartInfo.CreateNoWindow = false;
        process.Start();
        process.WaitForExit();
        return true;
    }

    public bool BackUpByDateTime(string serverAddress, int port, string userName, string password, string savePath, string dbName, string time)
    {
        if (string.IsNullOrEmpty(this.pg_dump))
            return false;

        Process process = new Process();
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
        process.StartInfo.Environment.Add("PGPASSWORD", password);
        process.StartInfo.FileName = this.pg_dump;
        process.StartInfo.Arguments = string.Format($"-h {serverAddress} -p {port} -U {userName} -F c -v -f \"{savePath}\" --table {time}* {dbName}");
        process.StartInfo.CreateNoWindow = false;
        process.Start();
        process.WaitForExit();
        return true;
    }
}
