//
// Copyright (c) 2016, MindFusion LLC - Bulgaria.
//

using System;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

using MindFusion.Spreadsheet;


namespace MindFusion.Spreadsheet.WinForms.Samples.CS.Calendar
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();

			workbookView.DrawFilledCellBorders = true;
			workbookView.AllowMoveCells = false;
			workbookView.AllowMoveHeaders = false;

			var activeSheet = workbookView.ActiveWorksheet;
			activeSheet.BeginInit();
			var heading = activeSheet.CellRanges["B2:H2"];
			heading.Merge();
			var headingCell = activeSheet.Cells["B2"];
			headingCell.Data = DateTime.Today;
			var headingStyle = headingCell.Style;
			headingStyle.Format = "MMMM, yyyy";
			headingStyle.HorizontalAlignment = HorizontalAlignment.Center;
			headingStyle.VerticalAlignment = VerticalAlignment.Middle;
			headingStyle.Background = new MindFusion.Drawing.SolidBrush(Color.SteelBlue);
			headingStyle.FontSize = 14;
			headingStyle.FontBold = true;
			headingStyle.TextColor = Color.White;
			var daysOfWeek = new CultureInfo("en-US").DateTimeFormat.AbbreviatedDayNames;
			for (int i = 0; i < 7; i++)
				activeSheet.Cells[i + 1, 2].Data = daysOfWeek[i];
			var subheading = activeSheet.CellRanges["B3:H3"];
			var subheadingStyle = subheading.Style;
			subheadingStyle.Background = new MindFusion.Drawing.SolidBrush(Color.FromArgb(245, 230, 205));
			subheadingStyle.HorizontalAlignment = HorizontalAlignment.Center;
			subheadingStyle.VerticalAlignment = VerticalAlignment.Middle;
			subheadingStyle.FontBold = true;
			var content = activeSheet.CellRanges["B4:H9"];
			var contentStyle = content.Style;
			contentStyle.Background = new MindFusion.Drawing.SolidBrush(Color.FromArgb(255, 250, 220));
			contentStyle.HorizontalAlignment = HorizontalAlignment.Center;
			contentStyle.VerticalAlignment = VerticalAlignment.Middle;
			contentStyle.Format = "D";
			var formula = "=IF(MONTH(DATE(YEAR(B2),MONTH(B2),1))<>MONTH(DATE(YEAR(B2),MONTH(B2),1)-(WEEKDAY(DATE(YEAR(B2),MONTH(B2),1))-1)+(ROW()-4)*7+(COLUMN()-1)-1),\"\",DATE(YEAR(B2),MONTH(B2),1)-(WEEKDAY(DATE(YEAR(B2),MONTH(B2),1))-1)+(ROW()-4)*7+(COLUMN()-1)-1)";
			for (int i = 0; i < 7; i++)
			{
				for (int j = 0; j < 6; j++)
					activeSheet.Cells[content.Left + i, content.Top + j].Data = formula;
			}
			activeSheet.Columns[1, 7].Width = "30pt";
			activeSheet.Rows[1].Height = "20pt";
			activeSheet.Rows[2, 8].Height = "16pt";
			activeSheet.EndInit();
		}
	}
}
