﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebUI
{
    public partial class _default : System.Web.UI.Page
    {
        HashTagDBDataContext dc = new HashTagDBDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshData();
        }

        protected void btnAnalyze_Click(object sender, EventArgs e)
        {
            string hashText = txtHashtag.Text;

            try
            {
                //Check if the hashtag exists
                hashtag ht = dc.hashtags.Where(x => x.HashTag.Contains(hashText)).FirstOrDefault();

                //If the hashtag doesn't exist, add it.
                if (ht == null) {
                    dc.hashtags.InsertOnSubmit(new hashtag { HashTag = hashText });
                    dc.SubmitChanges();
                }

                RefreshData();
            }
            catch (Exception ex)
            {
                Trace.Warn(ex.Message);
            }
        }

        private void RefreshData()
        {
            grdHashtags.DataSource = dc.hashtags.ToArray();
            grdHashtags.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            dc.ExecuteCommand("Delete from hashtags");
            RefreshData();
        }
    }
}