using System;
using System.Windows.Forms;

namespace Truffles
{
    public partial class CreateColumn : Form
    {
        frmMain mainfrm;
        public CreateColumn(frmMain main)
        {
            mainfrm = main;
            InitializeComponent();
        }

        public void init()
        {
            if (mainfrm.file.table == null) return;
            txtInfo.Text = mainfrm.file.table.Rows.Count + " rows will be created";
            cmbTypes.Items.Clear();
            cmbTypes.Items.Add("24: string (enter len)");
            cmbTypes.Items.Add("13: short");
            cmbTypes.Items.Add("03: uint");
            cmbTypes.Items.Add("02: ushort");
            cmbTypes.Items.Add("01: byte");
            cmbTypes.SelectedIndex = cmbTypes.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 len");
                    return;
                }
                mainfrm.file.CreateColumn(txtColName.Text, len, uint.Parse(cmbTypes.Text.Substring(0, 2)), txtDefault.Text);
            }
            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();
        }
    }
}