using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using C1.Win.C1Ribbon;
using C1.Win.FlexReport;
using C1.Win.C1Command;

namespace FlexCommonTasks
{
    public partial class Form1 : C1RibbonForm
    {
        const string c_FlexCommonTasks = "FlexCommonTasks.flxr";

        public Form1()
        {
            InitializeComponent();

            UpdateLayout();

            BuildTopicBar();
        }

        private void UpdateLayout()
        {
            tbReports.Parent = null;
            splitter1.Parent = null;

            var viewerControls = c1FlexViewer1.Controls;
            var c1 = viewerControls[0];
            var c2 = viewerControls[1];
            var c3 = viewerControls[2];
            viewerControls.Clear();

            viewerControls.Add(c1);
            viewerControls.Add(splitter1);
            viewerControls.Add(tbReports);
            viewerControls.Add(c2);
            viewerControls.Add(c3);
        }

        private void BuildTopicBar()
        {
            // get list of the reports
            string fileName = @"..\..\" + c_FlexCommonTasks;
            string[] reports = C1FlexReport.GetReportList(fileName);
            
            // go over reports and get category from C1FlexReport.ReportInfo.Keywords
            using (C1FlexReport fr = new C1FlexReport())
            {
                Dictionary<string, List<string>> categories = new Dictionary<string, List<string>>(StringComparer.CurrentCultureIgnoreCase);
                foreach (string reportName in reports)
                {
                    fr.Load(fileName, reportName);

                    string keywords = fr.ReportInfo.Keywords;
                    string[] ss = keywords.Split(new string[] { "\r\n" }, StringSplitOptions.None); 
                    for (int i = 0; i < ss.Length; i++)
                    {
                        List<string> reps;
                        if (!categories.TryGetValue(ss[i], out reps))
                        {
                            reps = new List<string>();
                            categories.Add(ss[i], reps);
                        }
                        reps.Add(reportName);
                    }
                }

                foreach (KeyValuePair<string, List<string>> kvp in categories)
                {
                    // build items in the C1TopicBar
                    C1TopicPage tp = new C1TopicPage(kvp.Key);
                    foreach (string s in kvp.Value)
                    {
                        C1TopicLink tl = new C1TopicLink(s);
                        tp.Links.Add(tl);
                    }
                    tbReports.Pages.Add(tp);
                }
            }
        }

        private void tbReports_LinkClick(object sender, C1TopicBarClickEventArgs e)
        {
            if (c1FlexReport1.IsBusy)
                return;
            c1FlexReport1.Load(@"..\..\" + c_FlexCommonTasks, e.Link.Text);
            c1FlexViewer1.DocumentSource = c1FlexReport1;
        }
    }
}
