//------------------------------------------------------------------------------
// written by: Shivangi C
// assisted by: NA
// debugged by:Shivangi C
//
// summary: This file has the binding of the UI controls and display with the 
//          bussiness layer functions for the Page which gives all the different
//          information about the stock selected like financial info, competitive 
//          stocks, predictions short term(14 days), long term (60 days) and 
//          next day, day performance and month performance along with the current 
//          information abou the stock
//          
//------------------------------------------------------------------------------


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace StockForecasterClient
{
    public partial class Performance : System.Web.UI.Page
    {
        /// <summary>
        /// make the webservice object to call the web methods required
        /// </summary>
        SFSService.StockInfo serviceObj;
        string stock_id;
        DataSet compFinancialInfo;

        /// <summary>
        /// The functions to be called and controls to be loaded on Page load event 
        /// are initialize/called here
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            serviceObj = new SFSService.StockInfo();
            serviceObj.Timeout = -1;
            GridView2.Visible = false;
            GridView3.Visible = false;
            GridView3.HeaderStyle.BackColor = System.Drawing.Color.LemonChiffon;
            stock_id = (string)Request.QueryString["ID"];
            DataSet compinfo;
            compinfo = (DataSet)serviceObj.GetGeneralStockInfoDAL(stock_id);
            GridView1.DataSource = compinfo;
            GridView1.DataBind();

        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Code for on Link Click event for the getFinancial Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkBtnGetCompanyInfo_Click(object sender, EventArgs e)
        {
            lnkBtnGetCompanyInfo.ForeColor= System.Drawing.Color.Purple;
            DataSet compFinancialInfo;
            GridView3.Visible = true;
            // call to webservice method GetFinancialInfo
            compFinancialInfo = (DataSet)serviceObj.GetFinancialInfo(stock_id);
            GridView3.DataSource = compFinancialInfo;
            // binding data to the grid for display of information
            GridView3.DataBind();
            LblPredictPrice.Font.Bold = true;
            LblPredictPrice.Text = "All Values are in Billion $";
            
        }
        /// <summary>
        /// Code for on Link Click event for the getDayPerformance Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButDayPerformance_Click(object sender, EventArgs e)
        {
            lnkButDayPerformance.ForeColor = System.Drawing.Color.Purple;
            DataSet compDayPerfInfo;
            LblPredictPrice.Visible = false;
            GridView3.Visible = true;
            // call to webservice method GetCurrentDayPerformance
            compDayPerfInfo = (DataSet)serviceObj.GetCurrentDayPerformance(stock_id);
            GridView3.DataSource = compDayPerfInfo;
            // binding data to the grid for display of information
            GridView3.DataBind();
        }
        /// <summary>
        /// Code for on Link Click event for the getMonthPerformance Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButMonthPerformance_Click(object sender, EventArgs e)
        {
            lnkButMonthPerformance.ForeColor = System.Drawing.Color.Purple;
            LblPredictPrice.Visible = false;
            DataSet compMonthPerfInfo;
            GridView3.Visible = true;
            // call to webservice method GetStockPerformance
            compMonthPerfInfo = (DataSet)serviceObj.GetStockPerformance(stock_id,30);
            GridView3.AlternatingRowStyle.BackColor = System.Drawing.Color.AliceBlue;
            GridView3.HeaderStyle.BackColor = System.Drawing.Color.LemonChiffon;
            GridView3.DataSource = compMonthPerfInfo;
            // binding data to the grid for display of information
            GridView3.DataBind();            
            
        }

        /// <summary>
        /// Code for on Link Click event for the Short Term prediction Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButShortSpecs_Click(object sender, EventArgs e)
        {
            LblPredictPrice.Visible = true;
            lnkButShortSpecs.ForeColor = System.Drawing.Color.Purple;
            decimal nextPrice;
            // call to webservice method GetSpeculativePrice
            nextPrice = serviceObj.GetSpeculativePrice(stock_id,0);
            LblPredictPrice.Text = "The predicted price of the stock after 14 days is " + (float)nextPrice;
        }
        /// <summary>
        /// Code for on Link Click event for the Long Term prediction Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButLongTerm_Click(object sender, EventArgs e)
        {
            LblPredictPrice.Visible = true;
            lnkButLongTerm.ForeColor = System.Drawing.Color.Purple;
            decimal nextPrice;
            // call to webservice method GetSpeculativePrice
            nextPrice = serviceObj.GetSpeculativePrice(stock_id, 1);
            LblPredictPrice.Text = "The long term predicted price of the stock after 60 days is " + (float)nextPrice;
        }
        
        /// <summary>
        /// Code for on Link Click event for the getting trading tips based on prediction Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButTradingTips_Click(object sender, EventArgs e)
        {
            LblPredictPrice.Visible = true;
            lnkButTradingTips.ForeColor = System.Drawing.Color.Purple;
            string tradingTips;
            int termCode = 0;
            if (RadioButton1.Checked || RadioButton2.Checked)
            {
                if (RadioButton1.Checked)
                    termCode = 0;
                else if (RadioButton2.Checked)
                    termCode = 1;
                // call to webservice method GetTradingTips
                tradingTips = serviceObj.GetTradingTips(stock_id, termCode);
                LblPredictPrice.Text = tradingTips;
                
            }
         }

        /// <summary>
        /// Code for on Link Click event for the getting Competitive Stock Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButCompetiveStocks_Click(object sender, EventArgs e)
        {
            lnkButCompetiveStocks.ForeColor = System.Drawing.Color.Purple;
            LblPredictPrice.Visible = false;
            
            GridView2.Visible = true;
            // call to webservice method GetCompetitiveStocks
            compFinancialInfo = (DataSet)serviceObj.GetCompetitiveStocks(stock_id);
            GridView2.DataSource = compFinancialInfo;
            // binding data to the grid for display of information
            GridView2.DataBind();
            GridView2.AlternatingRowStyle.BackColor = System.Drawing.Color.AliceBlue;
            GridView2.HeaderStyle.BackColor = System.Drawing.Color.LemonChiffon;

           

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Code for on Link Click event for the getting Stock Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButStockPriceInfo_Click(object sender, EventArgs e)
        {
            lnkButStockPriceInfo.ForeColor = System.Drawing.Color.Purple;
            LblPredictPrice.Visible = false;
            DataSet compFinancialInfo;
            GridView3.Visible = true;
            // call to webservice method GetCurrentDayPerformance to get the current day performance
            compFinancialInfo = (DataSet)serviceObj.GetStockPriceInfo(stock_id);
            GridView3.DataSource = compFinancialInfo;
            // binding data to the grid for display of information
            GridView3.DataBind();

        }

        /// <summary>
        /// Code for on Link Click event for the getting Next Day prediction Info functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkNextDayPredict_Click(object sender, EventArgs e)
        {
            LblPredictPrice.Visible = true;
            lnkNextDayPredict.ForeColor = System.Drawing.Color.Purple;
            decimal nextPrice;
            // call to webservice method GetNextSpeculativePrice to get next day prediction
            nextPrice = serviceObj.GetNextSpeculativePrice(stock_id);
            LblPredictPrice.Text = "The predicted price for tomorrow is " + (float)nextPrice;
        }

        /// <summary>
        /// This function creates dynamic Link functionality for all the competitve 
        /// stocks that are displayed for a particular sector
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                int index = e.Row.RowIndex;
                string ID = compFinancialInfo.Tables[0].Rows[index][0].ToString();
                if (e.Row.Cells[0].Controls.Count > 0)
                {
                    ((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "Performance.aspx?ID=" + ID;
                }
            }
        }

        /// <summary>
        /// This is a code to goback to the search page from the performance page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkButBackToSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("ForecasterClient.aspx");
        }
    }
}
