using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Web.UI.DataVisualization.Charting;
using System.Data.OleDb;
using System.Web.Configuration;
using System.Data.SqlClient;

public partial class code_mypage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //date calculator on page load
        string aa = from.Text;
        if (aa.Length < 1)
        {
            DateTime now;
            now = DateTime.UtcNow;
            now = now.AddHours(+1);
            to.Text = now.ToString();
            int a;
            a = Convert.ToInt32(tempo.Text);
            now = now.AddMinutes(-a);
            from.Text = now.ToString();
        }

        //Show username in two label
        myuser.Text = Context.User.Identity.Name.ToString();
        userdestra.Text = Context.User.Identity.Name.ToString();

        //if user is administrator then show AdminArea button
        if (User.IsInRole("administrators"))
            admin.Visible = true;

        //Grid sorting
        GridView1.Sort("id", SortDirection.Descending);
    }

    protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }

    protected void admin_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/DefaultAdmin.aspx");
    }
    protected void refresh_Click(object sender, EventArgs e)
    {
        //datbinding on refresh... to improve

        //id.DataBind();
        //AccessDataSource2.DataBind();
        tempo.DataBind();
        from.DataBind();
        to.DataBind();
        AccessDataSource3.DataBind();
        GridView1.DataBind();
        Chart1.DataBind();
    }

    protected void from_TextChanged(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    protected void to_TextChanged(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    protected void tempo_SelectedIndexChanged(object sender, EventArgs e)
    {
        //date calculator 
        DateTime now;
        now = DateTime.UtcNow;
        now = now.AddHours(+1);
        to.Text = now.ToString();
        int a;
        a = Convert.ToInt32(tempo.Text);
        now = now.AddMinutes(-a);
        from.Text = now.ToString();

        //Chart X axis changing
        if (tempo.SelectedIndex > 9)
        {
            Chart1.Series[0].XValueType = ChartValueType.DateTime;
            Chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 45;
            // Chart1.Series[0].ChartType = SeriesChartType.Spline;
        }
    }

    protected void Chart1_Load(object sender, EventArgs e)
    {
        //databind on load
        if (id.Text.Equals(""))
        {
            AccessDataSource2.DataBind();
            id.DataBind();
            id.SelectedIndex = 0;
        }

        //***Reading description***
        string title = "";
        OleDbConnection conn = null;
        OleDbCommand command = null;
        OleDbDataReader reader;
        string another = WebConfigurationManager.ConnectionStrings["AccessFileName"].ToString();
        string sql = "SELECT description FROM datastreams WHERE (id_datastream = @ourid)";
        try
        {
            conn = new OleDbConnection(another);
            conn.Open();
            command = new OleDbCommand(sql, conn);
            command.Parameters.AddWithValue("@ourid", id.Text);
            reader = command.ExecuteReader();
            //description reading
            while (reader.Read())
            {
                title = reader["description"].ToString();
            }
            reader.Close();
        }

        finally
        {
            conn.Close();
        }

        //titles is description
        titolo.Text = title.ToString();
        Chart1.Titles[0].Text = titolo.Text;
    }
    protected void spline_Click(object sender, EventArgs e)
    {
        int a = 0, b = 0, result = 0;
        a = GridView1.Rows.Count;
        b = GridView1.PageCount;
        result = a * b;

        if ((spline.Text == "Spline") && (result < 500))
        {
            Chart1.Series[0].ChartType = SeriesChartType.Spline; //per cambiare tipo di grafico
            spline.Text = "DeSpline";
        }
        else
        {
            spline.Text = "Spline";
            Chart1.Series[0].ChartType = SeriesChartType.Line;
        }
    }
    protected void button_3d_Click(object sender, EventArgs e)
    {
        int a = 0, b = 0, result = 0;
        a = GridView1.Rows.Count;
        b = GridView1.PageCount;
        result = a * b;
        if ((button_3d.Text == "3D") && (result < 500))
        {
            Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
            //Chart1.ChartAreas[0].Area3DStyle.Rotation = 80;
            button_3d.Text = "De3D";
        }
        else
        {
            Chart1.ChartAreas[0].Area3DStyle.Enable3D = false;
            button_3d.Text = "3D";
        }
    }
}