using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace RPGMakerXPLauncher
{
    public partial class MainForm : Form
    {
        private string updateUrl = "https://example.com/updates/latest.zip"; // URL to download the latest update
        private string localVersionFile = "version.txt"; // Local version file
        private string remoteVersionFileUrl = "https://example.com/updates/version.txt"; // URL to check the latest version
        public MainForm()
        {
            InitializeComponent();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            CheckForUpdates();
        }
        private void btnLaunchGame_Click(object sender, EventArgs e)
        {
            string gamePath = txtGamePath.Text;
            if (File.Exists(gamePath))
            {
                try
                {
                    Process.Start(gamePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred while trying to launch the game: " + ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Game executable not found. Please check the path.");
            }
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Executable Files (*.exe)|*.exe";
                openFileDialog.Title = "Select RPG Maker XP Game Executable";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtGamePath.Text = openFileDialog.FileName;
                }
            }
        }
        private void CheckForUpdates()
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    string remoteVersion = client.DownloadString(remoteVersionFileUrl).Trim();
                    string localVersion = File.Exists(localVersionFile) ? File.ReadAllText(localVersionFile).Trim() : "0";
                    if (remoteVersion != localVersion)
                    {
                        DialogResult dialogResult = MessageBox.Show("An update is available. Do you want to download it now?", "Update Available", MessageBoxButtons.YesNo);
                        if (dialogResult == DialogResult.Yes)
                        {
                            DownloadUpdate();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while checking for updates: " + ex.Message);
            }
        }
        private void DownloadUpdate()
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    string tempFile = Path.GetTempFileName();
                    client.DownloadFile(updateUrl, tempFile);
                    string extractPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update");
                    System.IO.Compression.ZipFile.ExtractToDirectory(tempFile, extractPath);
                    // Replace old files with new ones
                    foreach (string file in Directory.GetFiles(extractPath))
                    {
                        string destFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(file));
                        File.Copy(file, destFile, true);
                    }
                    // Update local version file
                    string remoteVersion = client.DownloadString(remoteVersionFileUrl).Trim();
                    File.WriteAllText(localVersionFile, remoteVersion);
                    MessageBox.Show("Update completed successfully!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while downloading or applying the update: " + ex.Message);
            }
        }
        private void InitializeComponent()
        {
            this.txtGamePath = new System.Windows.Forms.TextBox();
            this.btnLaunchGame = new System.Windows.Forms.Button();
            this.btnBrowse = new System.Windows.Forms.Button();
            this.lblGamePath = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // txtGamePath
            // 
            this.txtGamePath.Location = new System.Drawing.Point(12, 25);
            this.txtGamePath.Name = "txtGamePath";
            this.txtGamePath.Size = new System.Drawing.Size(360, 20);
            this.txtGamePath.TabIndex = 0;
            // 
            // btnLaunchGame
            // 
            this.btnLaunchGame.Location = new System.Drawing.Point(297, 51);
            this.btnLaunchGame.Name = "btnLaunchGame";
            this.btnLaunchGame.Size = new System.Drawing.Size(75, 23);
            this.btnLaunchGame.TabIndex = 1;
            this.btnLaunchGame.Text = "Launch Game";
            this.btnLaunchGame.UseVisualStyleBackColor = true;
            this.btnLaunchGame.Click += new System.EventHandler(this.btnLaunchGame_Click);
            // 
            // btnBrowse
            // 
            this.btnBrowse.Location = new System.Drawing.Point(216, 51);
            this.btnBrowse.Name = "btnBrowse";
            this.btnBrowse.Size = new System.Drawing.Size(75, 23);
            this.btnBrowse.TabIndex = 2;
            this.btnBrowse.Text = "Browse...";
            this.btnBrowse.UseVisualStyleBackColor = true;
            this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
            // 
            // lblGamePath
            // 
            this.lblGamePath.AutoSize = true;
            this.lblGamePath.Location = new System.Drawing.Point(12, 9);
            this.lblGamePath.Name = "lblGamePath";
            this.lblGamePath.Size = new System.Drawing.Size(58, 13);
            this.lblGamePath.TabIndex = 3;
            this.lblGamePath.Text = "Game Path";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 86);
            this.Controls.Add(this.lblGamePath);
            this.Controls.Add(this.btnBrowse);
            this.Controls.Add(this.btnLaunchGame);
            this.Controls.Add(this.txtGamePath);
            this.Name = "MainForm";
            this.Text = "RPG Maker XP Launcher";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        private System.Windows.Forms.TextBox txtGamePath;
        private System.Windows.Forms.Button btnLaunchGame;
        private System.Windows.Forms.Button btnBrowse;
        private System.Windows.Forms.Label lblGamePath;
    }
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}