using System.IO;
using System.Text;
using Netch.Models;
using Netch.Servers.Socks5;
using Netch.Utils.HttpProxyHandler;

namespace Netch.Controllers
{
    public class PrivoxyController : Guard, IController
    {
        public PrivoxyController()
        {
            RedirectStd = false;
        }

        public override string Name { get; protected set; } = "Privoxy";

        public override string MainFile { get; protected set; } = "Privoxy.exe";

        public bool Start(Server server, Mode mode)
        {
            var text = new StringBuilder(File.ReadAllText("bin\\default.conf"));

            text.Replace("_BIND_PORT_", Global.Settings.HTTPLocalPort.ToString());
            text.Replace("0.0.0.0", Global.Settings.LocalAddress); /* BIND_HOST */

            if (server is Socks5 socks5 && !socks5.Auth())
            {
                text.Replace("/ 127.0.0.1", $"/ {server.AutoResolveHostname()}"); /* DEST_HOST */
                text.Replace("_DEST_PORT_", socks5.Port.ToString());
            }

            text.Replace("_DEST_PORT_", Global.Settings.Socks5LocalPort.ToString());


            File.WriteAllText("data\\privoxy.conf", text.ToString());

            return StartInstanceAuto("..\\data\\privoxy.conf");
        }

        public override void Stop()
        {
            StopInstance();
        }
    }
}