using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using X360.STFS;
using X360.IO;
using X360.Security.Cryptography;

namespace Last_Remnant_Save_Editor
{
    public partial class Form1 : Form
    {
        //string fileName;
        STFSPackage package;
        FileEntry fileinfo;
        DJsIO savedata;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                STFSPackage xpack = new STFSPackage(null);
                if (!xpack.ParseSuccess)
                    return;
                FileEntry xEnt = xpack.GetFile("savegame00");
                DJsIO xSaveIO = new DJsIO(EndianType.BigEndian);
                if (!xEnt.Extract(xSaveIO))
                {
                    MessageBox.Show("Error: Save Data Not Found!!");
                    return;
                }
                fileinfo = xEnt;
                savedata = xSaveIO;
                package = xpack;
                MessageBox.Show("Save successfully loaded");
            }
            catch
            {
                MessageBox.Show("Not a valid Remnant Save!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (package == null)
                MessageBox.Show("No Remnant Save Opened!");

                    

            if (package == null)
                return;
            if (checkBox1.Checked)
            {
                savedata.Position = 0x1D7A4;
                savedata.Write((int)1000000);
            }
            //Max Out Current Money!
            savedata.Flush();
            /* Security fix section */
            savedata.Position = 32;
            byte[] Data = savedata.ReadBytes(1628048);
            byte[] Hash = SHA1Quick.ComputeHash(Data);
            savedata.Position = 12;
            savedata.Write(Hash);
            savedata.Flush();
            if (!fileinfo.Replace(savedata))
            {
                MessageBox.Show("Error when saving the data to the gamesave");
                return;
            }
            package.Header.MakeAnonymous();
            if (!package.FlushPackage(new RSAParams(Application.StartupPath + "/KV.bin")))
            {
                MessageBox.Show("Error when fixing package");
                return;
            }
            MessageBox.Show("Your Last Remnant Save Has Been Modded + Fixed!");
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void extractSaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (package == null)
                MessageBox.Show("No Save Data Loaded To Extract!");


            if (package == null) // Prevents an error if someone clicks this before they open a save
                return;
            DJsIO xout = new DJsIO(DJFileMode.Create, "Save to where?", "", EndianType.BigEndian);
            if (!xout.Accessed)
                return;
            fileinfo.Extract(xout);
            MessageBox.Show("Done extracting save data.");
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 x = new AboutBox1();
            x.ShowDialog();
        }

        private void applicationToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void tutorialToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox2 x = new AboutBox2();
            x.ShowDialog();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process xlink = new Process();
            xlink.StartInfo.FileName = "http://www.xbox360-content.com/forums";
            xlink.Start();
        }

        private void visitUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }


    }
}


                
            
        
 



 