/* **********************************************************************************
 *
 * Copyright (c) Microsoft Corporation. All rights reserved.
 *
 * This source code is subject to terms and conditions of the Shared Source License
 * for IronPython. A copy of the license can be found in the License.html file
 * at the root of this distribution. If you can not locate the Shared Source License
 * for IronPython, please send an email to ironpy@microsoft.com.
 * By using this source code in any fashion, you are agreeing to be bound by
 * the terms of the Shared Source License for IronPython.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * **********************************************************************************/

using IronPython.Hosting;
using IronPython.Compiler;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace HostingApp {
    public partial class MonthAtAGlance : Form {

        // Application variables
        private Dictionary<System.DateTime, string> dateEntries;
        private System.DateTime lastDate;

        private PythonEngine engine;

        public MonthAtAGlance() {
            InitializeComponent();
            InitializeApp();
            InitializePythonEngine();
        }

        void InitializePythonEngine() {
            engine = new PythonEngine();
            engine.ImportSite();
            System.IO.FileStream fs = new System.IO.FileStream("scripting-log.txt",
              System.IO.FileMode.Create);
            engine.SetStdout(fs);
            engine.SetStderr(fs);
            engine.SetVariable("dateentries", dateEntries);
            try {
                engine.Execute("print 'Hello world!'");
            } catch (IronPython.Runtime.PythonNameError exc) {
                MessageBox.Show("IronPython engine complained: " + exc.Message);
            } catch (Exception exc) {
                MessageBox.Show("Unexpected exception " + exc.ToString() + "\n Shutting down!");
                System.Environment.Exit(-1);
            }
        }


        public delegate void DateTextChangedEventHandler(DateTime day, string newText);

        public event DateTextChangedEventHandler EntryChangedHandler;

        private void defaultEvtHandler(DateTime day, string nt) {

        }


        private void InitializeApp() {
            dateEntries = new Dictionary<DateTime, string>();
            lastDate = monthCalendar.TodayDate;
            EntryChangedHandler = new DateTextChangedEventHandler(defaultEvtHandler);


        }

        private bool dateSubmitEvent(DateTime dt, string txt) {
            string oldDateText; // current text in the date we are leaving
            // if txt != this, place txt in the date we are leaving
            string newDateText; // current text in the date we are entering
            // to be moved to textBox

            bool wasUpdate = true;

            if (!dateEntries.TryGetValue(dt, out newDateText))
                newDateText = "";
            textBox.Text = newDateText;

            if (!dateEntries.TryGetValue(lastDate, out oldDateText))
                oldDateText = "";

            if (oldDateText == txt)
                wasUpdate = false;

            else {
                if (dateEntries.ContainsKey(lastDate))
                    dateEntries.Remove(lastDate);
                if (txt != "")
                    dateEntries.Add(lastDate, txt);
                else
                    wasUpdate = false;
            }

            lastDate = dt;
            return wasUpdate;
        }

        private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e) {
            DateTime changedTo = e.End;
            string currText = textBox.Text;
            if (dateSubmitEvent(changedTo, currText) &&
                EntryChangedHandler != null)
                EntryChangedHandler(changedTo, currText);

            engine.Execute("n = dateentries.Count");
            int nEntries = engine.Evaluate<int>("n");
            MessageBox.Show("There are now " + nEntries.ToString() + " entries in the dictionary.");

        }
    }
}