//	Spludlow Software
//	Copyright © Samuel P. Ludlow 2020 All Rights Reserved
//	Distributed under the terms of the GNU General Public License version 3
//	Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
//	https://www.spludlow.co.uk/LICENCE.TXT
//	The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission
//	v1.14

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;


public partial class View : System.Web.UI.Page
{
	private DataSet _DataSet;

    protected void Page_Load(object sender, EventArgs e)
    {
		Spludlow.WebControls.DisableOnClick(this.ButtonQuery);

		if (this.IsPostBack == false)
		{
			this.DropDownListDataProductId.DataSource = Spludlow.MetSat.View.Products();
			this.DropDownListDataProductId.DataTextField = "Name";
			this.DropDownListDataProductId.DataValueField = "DataProductId";
			this.DropDownListDataProductId.DataBind();

			DateTime selectDate = DateTime.Now.Date.AddDays(-1);

			this.Calendar1.SelectedDate = selectDate;
			this.Calendar2.SelectedDate = selectDate;
		}

    }
	protected void ButtonQuery_Click(object sender, EventArgs e)
	{
		DateTime startDate = this.Calendar1.SelectedDate.Date;
		DateTime beforeEndDate = this.Calendar2.SelectedDate.Date.AddDays(1);
				
		this._DataSet = Spludlow.MetSat.View.Query(this.DropDownListDataProductId.SelectedValue, startDate, beforeEndDate);

		this.DataListItems.DataSource = this._DataSet.Tables["DataItems"];
		this.DataListItems.DataBind();

		this.LabelError.Text = "Data Items: " + this._DataSet.Tables["DataItems"].Rows.Count.ToString();

	}
	protected void DataListItems_ItemDataBound(object sender, DataListItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
			return;

		DataRow row = ((DataRowView)e.Item.DataItem).Row;

		DataView view;
		StringBuilder text;
		GridView gridView;

		text = new StringBuilder();
		text.Append("DataItemId = '");
		text.Append((long)row["DataItemId"]);
		text.Append("'");

		string filter = text.ToString();

		view = new DataView(this._DataSet.Tables["DataItems"]);
		view.RowFilter = filter;
		gridView = (GridView)e.Item.FindControl("GridViewItem");
		gridView.DataSource = view;
		gridView.DataBind();

		view = new DataView(this._DataSet.Tables["DataItemFiles"]);
		view.RowFilter = filter;
		view.Sort = "DataItemFileTypeId, Channel, Filename";
		gridView = (GridView)e.Item.FindControl("GridViewFiles");
		gridView.DataSource = view;
		gridView.DataBind();

		view = new DataView(this._DataSet.Tables["DataItemFiles"]);
		view.RowFilter = filter + " AND Pixels <> 0";
		view.Sort = "DataItemFileTypeId, Channel, Filename";
		DataList dataList = (DataList)e.Item.FindControl("DataListFiles");
		dataList.DataSource = view;
		dataList.DataBind();
	}
}