using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace CobProgAid
{
    public partial class SaveRegCopyFile : Form
    {
        public savedFile SavedFile { get; set; }
        public string FileData { get; set; }
        public string finalFileName { get; set; }

        public SaveRegCopyFile()
        {
            InitializeComponent();
        }

        private void btnChooseFnameAndPlace_Click(object sender, EventArgs e)
        {
            saveFileDialog.Title = "Välj plats för den aktuella filen !";
            saveFileDialog.Filter = "Copy files (*.cpy)|*.cpy|Cobol code (*.cbl)|*.cbl|Text Files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog.FileName = SavedFile.fNameSaved;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                SavedFile.fNameSaved = txtFileName.Text = saveFileDialog.FileName.Substring(saveFileDialog.FileName.LastIndexOf("\\") + 1); ;
                SavedFile.directorySaved = txtChosenDirectory.Text = saveFileDialog.FileName.Substring(0, saveFileDialog.FileName.LastIndexOf("\\"));
                SavedFile.usageName = txtUsageName.Text;
                if (SavedFile.idConnect.Length > 0)
                {
                    btnSave.Enabled = true;
                }
                else MessageBox.Show("idConnect för filtypen saknas !");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.finalFileName = saveFileDialog.FileName;
                File.WriteAllText(saveFileDialog.FileName, FileData);
                SavedFile.saveDate = DateTime.Now;
                savedFile tmpSF = new savedFile();
                //tmpSF = (savedFile)SavedFile.Clone();
                //S.ettingListAdd(tmpSF);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem att spara :\r\n" + ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveRegCopyFile_Load(object sender, EventArgs e)
        {
            txtUsageName.Text = SavedFile.usageName;
            txtChosenDirectory.Text = "";
            txtFileName.Text = "";
            btnSave.Enabled = false;

        }
    }
}
