//constructor called when creating new task
        public TaskForm(List list)
        {
            InitializeComponent();
            this.list = list;
            lblTaskName.Text = "Create new Task for Project " + list.Name;
            cmdUpdate.Text = "Create";

            typeOfAction = "create";
        }
        private void CreateNewList()
        {
            DatabaseHelper dbhelper = new DatabaseHelper();
            projectName = txtProjectName.Text;

            if (DataGood())
            {
                List newProject = new List(projectName);
                dbhelper.CreateList(newProject);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Enter a List Item Name", "List Item Add Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = System.Windows.Forms.DialogResult.Retry;
            }
        }
        public void CreateList(List list)
        {
            createCommand();

            string sql = "INSERT INTO [List] ([id], [name]) VALUES(@listID, @listName)";

            if (list.ID == -1)
            {
                list.ID = GetListCount();
            }

            _cmd.CommandText = sql;
            _cmd.Parameters.Add("@listID", System.Data.SqlDbType.Int).Value = list.ID;
            _cmd.Parameters.Add("@listName", System.Data.SqlDbType.VarChar).Value = list.Name;

            _conn.Open();
            _cmd.ExecuteNonQuery();
            _conn.Close();
        }
        void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmdViewTaskDetails.Enabled = false;
            cmdDeleteTask.Enabled = false;
            cmdCompleteTask.Enabled = false;

            listSelectedIndex = listBox1.SelectedIndex;

            if (listSelectedIndex != -1)
            {
                List selectedList = ((List)(listBox1.Items[listSelectedIndex]));

                //clear taskBox
                taskBox.Items.Clear();

                //set label
                lblProjectName.Text = selectedList.Name;

                //populate taskBox
                tasks = dbHelper.GetTasksForList(selectedList.ID);
                foreach (Task t in tasks)
                {
                    if (t.IsCompleted())
                    {
                        taskBox.Items.Add("[COMPLETED] " + t.Name);
                    }
                    else
                    {
                        taskBox.Items.Add("[INCOMPLETE] " + t.Name);
                    }
                }

                drawProgressBar();
            }
        }
        private void GetData()
        {
            dbHelper = new DatabaseHelper();
            allLists = dbHelper.GetLists();

            int previousIndex = listSelectedIndex;
            listBox1.DataSource = allLists;

            if ((previousIndex + 1) > allLists.Count)
            {
                if (allLists.Count == 0)
                {
                    listBox1.ClearSelected();
                    ResetInterface();
                }
                else
                {
                    listBox1.SelectedIndex = previousIndex - 1;
                }
            }
            else
            {
                listBox1.SelectedIndex = previousIndex;
            }
        }
        private void drawProgressBar()
        {
            listSelectedIndex = listBox1.SelectedIndex;
            int numberOfTasks;
            int numberOfCompletedTasks = 0;
            double percentageOfTasksCompleted = 0;
            List selectedList = ((List)(listBox1.Items[listSelectedIndex]));

            if (listSelectedIndex >= 0)
            {
                tasks = dbHelper.GetTasksForList(selectedList.ID);
                numberOfTasks = tasks.Count;

                foreach (Task t in tasks)
                {
                    if (t.IsCompleted())
                    {
                        numberOfCompletedTasks++;
                    }
                }

                try
                {
                    percentageOfTasksCompleted = ((float)numberOfCompletedTasks / (float)numberOfTasks) * 100.0;
                }
                catch (DivideByZeroException)
                {
                    percentageOfTasksCompleted = 100;
                }

                completionBar.BackColor = Color.Green;
                completionBar.Width = (int)(500 * (percentageOfTasksCompleted / 100));
            }
        }
        public void DeleteList(List list)
        {
            List<Task> tasks = GetTasksForList(list.ID);

            foreach (Task task in tasks)
            {
                DeleteTask(task);
            }

            createCommand();
            string sql = "DELETE FROM [List] WHERE [id] = @listId";
            _cmd.CommandText = sql;
            _cmd.Parameters.Add("@listId", System.Data.SqlDbType.Int).Value = list.ID;
            _conn.Open();
            _cmd.ExecuteNonQuery();
            _conn.Close();
        }
        public List<Task> GetTasksForList(int ListID)
        {
            createCommand();

            List<Task> tasks = new List<Task>();
            string sql = "SELECT * FROM [Task] INNER JOIN [List] ON [Task].[listID] = [List].[id] WHERE [List].[id] = @listID";
            _cmd.CommandText = sql;
            _cmd.Parameters.Add("@listID", System.Data.SqlDbType.Int).Value = ListID;

            _conn.Open();
            SqlDataReader dr = _cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while(dr.Read())
                {
                    int ID = Convert.ToInt32(dr["id"]);
                    int listID = Convert.ToInt32(dr["listID"]);
                    string name = dr["name"].ToString();
                    string description = dr["description"].ToString();
                    DateTime due = Convert.ToDateTime(dr["due"]);
                    DateTime completed = DateTime.MinValue;
                    //Yes this sucks, but it wants an ordinal :(
                    if (!dr.IsDBNull(5))
                    {
                         completed = Convert.ToDateTime(dr["completed"]);
                    }

                    Task task = new Task(listID, name, description, due, completed);
                    task.ID = ID;
                    tasks.Add(task);
                }
            }
            dr.Close();
            _conn.Close();

            return tasks;
        }
        //INCEPTION FUN!
        public List<List> GetLists()
        {
            createCommand();

            List<List> lists = new List<List>();

            string sql = "SELECT * FROM [List]";
            _cmd.CommandText = sql;

            _conn.Open();
            SqlDataReader dr = _cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    int id = Convert.ToInt32(dr["id"]);
                    string name = dr["name"].ToString();
                    lists.Add(new List(id, name));
                }
            }
            dr.Close();
            _conn.Close();

            return lists;
        }
        public List GetList(int ListID)
        {
            createCommand();
            List list = new List();

            string sql = "SELECT * FROM [List] WHERE [List].[id] = @listID";
            _cmd.CommandText = sql;
            _cmd.Parameters.Add("@listID", System.Data.SqlDbType.Int).Value = ListID;

            _conn.Open();
            SqlDataReader dr = _cmd.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                int id = Convert.ToInt32(dr["id"]);
                string name = dr["name"].ToString();
                list = new List(id, name);
            }
            dr.Close();
            _conn.Close();

            return list;
        }