using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    class RetrieveTables
    {
        private string MostRecentDate;
        
        public RetrieveTables()
        {
            if (MostRecentDate == null)
            {
                apcDatabaseTableAdapters.WeekEndingDateTableAdapter date = new apcDatabaseTableAdapters.WeekEndingDateTableAdapter();    
                MostRecentDate = date.GetMostRecentDate().ToString();
            }
            MostRecentDate = "1";
        }

        public DataTable APCProjectionSaleTable()
        {
            DataTable APCProjectionSaleTable = new DataTable();
            apcDatabaseTableAdapters.ProjectedSalesTableAdapter APCProjectionSaleTableAdapter = new apcDatabaseTableAdapters.ProjectedSalesTableAdapter();
            APCProjectionSaleTable = APCProjectionSaleTableAdapter.GetDataByProjections(MostRecentDate);
            return APCProjectionSaleTable;
        }

        public DataTable APCActualSalesTable()
        {
            DataTable APCActualSalesTable = new DataTable();
            apcDatabaseTableAdapters.ActualSalesTableAdapter APCActualSalesTableAdapter = new apcDatabaseTableAdapters.ActualSalesTableAdapter();
            APCActualSalesTable = APCActualSalesTableAdapter.ReturnInventoryAndActualSales(MostRecentDate);
            return APCActualSalesTable;
        }

        public string GetAdItemID(string adItemString)
        {
            string adItemID;
            apcDatabaseTableAdapters.AdItemTableAdapter adItemTableAdapter = new apcDatabaseTableAdapters.AdItemTableAdapter();
            adItemID = adItemTableAdapter.ReturnAddItemIDByItemString(adItemString).Rows[0][0].ToString();

            return adItemID;
        }

        public DataGridView WICTable()
        {
            DataGridView wicTable = new DataGridView();
            apcDatabaseTableAdapters.WeekEndingDateTableAdapter weekEndingTableAdapter = new apcDatabaseTableAdapters.WeekEndingDateTableAdapter();
            wicTable.DataSource = weekEndingTableAdapter.ReturnWICTable("1");
            return wicTable;
        }

        public string[] Comment()
        {
            DataGridView commentTable = new DataGridView();
            List<String> comments = new List<string>();
            
            apcDatabaseTableAdapters.CommentTableAdapter commentsTableAdapter = new apcDatabaseTableAdapters.CommentTableAdapter();
            commentTable.DataSource = commentsTableAdapter.GetDataByDateID(MostRecentDate);

            if (commentTable.Rows.Count > 0)
            {
                string[] finalCommentArray = new String[commentTable.Rows.Count];
                for (int i = 0; i < commentTable.Rows.Count; i++)
                {
                    finalCommentArray[i] = Convert.ToString(commentTable.Rows[i].Cells[0].Value);
                    comments.Add(finalCommentArray[i]);
                }
                return finalCommentArray;
            }
            else
            {
                comments.Add("No comments to display for the week ending on " + Date());
                return comments.ToArray();
            }
        }

        public string Date()
        {
            string dateString;
            apcDatabaseTableAdapters.WeekEndingDateTableAdapter weekEnding = new apcDatabaseTableAdapters.WeekEndingDateTableAdapter();
            DateTime dateTime = new DateTime();
            dateTime = (DateTime)weekEnding.ReturnDate(MostRecentDate).Rows[0][1];
            dateString = dateTime.ToLongDateString();
            return dateString;
        }

        public string DateID(string dateString)
        {
            apcDatabaseTableAdapters.WeekEndingDateTableAdapter weekEnding = new apcDatabaseTableAdapters.WeekEndingDateTableAdapter();
            string dateID;

            dateID = weekEnding.ReturnDateIDByDateString(dateString).Rows[0][0].ToString();

            return dateID;
        }
    }
}
