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 DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;

namespace UniversalEditor
{
    public partial class columnCreate : XtraForm
    {
        frmMain mainfrm;
        public columnCreate(frmMain main)
        {
            mainfrm = main;
            InitializeComponent();
        }

        public void init()
        {
            if (mainfrm.file.table == null) return;
            txtInfo.Text = mainfrm.file.table.Rows.Count - 1 + " cell(s) will be created.";
            // 26 = string (non fixed len)
            // 3 = uint
            // 1 = byte
            // 2 = ushort
            // 13 = short
            // 24 = string (fixed len)
            cmbTypes.Properties.Items.Clear();
            cmbTypes.Properties.Items.Add("24: String (Enter Length)");
            cmbTypes.Properties.Items.Add("13: Short");
            cmbTypes.Properties.Items.Add("03: uInt");
            cmbTypes.Properties.Items.Add("02: uShort");
            cmbTypes.Properties.Items.Add("01: Byte");
            cmbTypes.SelectedIndex = cmbTypes.Properties.Items.Count - 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int len = int.Parse(txtLen.Text);
                if (len < 0)
                {
                    MessageBox.Show("Please enter a valid length");
                    return;
                }
                if (mainfrm.file.columns[0].name == "Empty Column")
                {
                    mainfrm.file.DeleteColumn("Empty Column");
                }
                mainfrm.file.CreateColumn(txtColName.Text, len, uint.Parse(cmbTypes.Text.Substring(0, 2)), txtDefault.Text);
                mainfrm.dataGrid.MainView.PopulateColumns();
                string t = mainfrm.FileTabs.SelectedTabPage.Text;
                string n = t.Replace("*", String.Empty);
                mainfrm.FileTabs.SelectedTabPage.Text = n;
                mainfrm.FileTabs.SelectedTabPage.Text = mainfrm.FileTabs.SelectedTabPage.Text + "*";
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }         
        }

        public int GetDefLen(int type)
        {
            switch (type)
            {
                default:
                    return -1;
                case 13:
                    return 2;
                case 3:
                    return 4;
                case 2:
                    return 2;
                case 1:
                    return 1;    
            }
        }

        private void cmbTypes_TextChanged(object sender, EventArgs e)
        {
            byte type = byte.Parse(cmbTypes.Text.Substring(0, 2));
            txtLen.Text = GetDefLen(type).ToString();
        }
    }
}
