﻿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;

namespace Sigma
{
    public partial class EventEditor : Form
    {
        /// <summary>
        /// The event being modified
        /// </summary>
        private Event ev;

        private Series series;

        private ListViewItem lvi;

        bool cancelClose = true;

        public EventEditor(Event evsent, Series series, ListViewItem lvi)
        {
            InitializeComponent();
            frmMain.EditorOpened();

            this.series = series;
            this.lvi = lvi;
            this.Text = evsent.Title;

            ev = evsent;
            ev.Locked = true;

            PopulateControls();
        }

        private void PopulateControls()
        {
            txtTitle.Text = ev.Title;
            txtDate.Text = ev.Date;
            txtText.Text = ev.Text;
        }

        private void UpdateEvent()
        {
            ev.Title = txtTitle.Text;
            ev.Date = txtDate.Text;
            ev.Text = txtText.Text;
        }

        private void CloseForm()
        {
            ev.Locked = false;
            frmMain.EditorClosed();

            cancelClose = false;
            Close();
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (txtTitle.Text == "" && txtDate.Text == "")
            {
                MessageBox.Show("This event must have a name or a date.");
                return;
            }

            UpdateEvent();

            // If lvi==null, creating new character
            if (lvi == null)
            {
                Program.form.AddEvent(ev);
            }
            else
            {
                Program.form.UpdateEvent(ev, lvi);
            }

            CloseForm();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        private void EventEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = cancelClose;
        }

        private void txtTitle_Leave(object sender, EventArgs e)
        {
            if (txtTitle.Text == "")
                txtTitle.Text = ev.Text;
            else
                this.Text = txtTitle.Text;
        }
    }
}
