#if !ASPNETCORE
using IActionResult = System.Web.Http.IHttpActionResult;
#else
using Microsoft.AspNetCore.Mvc;
#endif
using C1.Web.Api.Report;
using WebApi.Models;
using System.Text;
using System.Collections;

namespace WebApi.Controllers
{
    /// <summary>
    /// Define a new controller which extends from <see cref="ReportController"/>.
    /// </summary>
    public class C1ReportController : ReportController
    {
        /// <summary>
        /// Overrides to response html content when accessing the "http://host/api/report" url in the browser.
        /// </summary>
        /// <returns>The html content.</returns>
        public override IActionResult GetCatalogInfo(string path, bool recursive = false)
        {
            if (C1Controller.IsIntroductionRequest(this))
            {
                return C1Controller.GetIntroductionPageResult(this, GetReport());
            }
            return base.GetCatalogInfo(path, recursive);
        }

        private string GetReportTree(string name, IEnumerable source)
        {
            var sbContent = new StringBuilder();
            sbContent.AppendLine(string.Format("<h5>{0}</h5>", name));
            sbContent.Append(PageTemplate.GetTree(source, name.Replace(" ", ""), "Title", "Children", true));
            return sbContent.ToString();
        }

        private string GetDetails()
        {
            var sbContent = new StringBuilder();
            sbContent.Append(GetReportTree("FlexReports", ReportFiles.Reports));
            sbContent.Append(GetReportTree("SSRS Reports", Documents.SsrsReports.Items));
            return sbContent.ToString();
        }

        private C1APIIntroduction GetReport()
        {
            var c1APIIntroduction = new C1APIIntroduction();
            c1APIIntroduction.Name = "Report";
            c1APIIntroduction.Abstract = "<p>Web API Studio Edition releases/ introduces Report Services that enables you to build HTTP services,"
                + " which can be consumed by a variety of clients for viewing, loading and caching FlexReports and SSRS reports."
                + " These are REST based API services, which communicate with HTML 5 ReportViewer control to display the report content on the web."
                + " You can view a variety of reports in the HTML5 based ReportViewer control, mainly FlexReport and SSRS reports.</p>"
                + "<p>Available as Visual Studio template, C1 Web API enables you to create reporting services on Visual Studio."
                + " Client applications send a request to the report service applications to load reports, export reports,"
                + " and allow users to use parameters in the report. The service supports exporting your reports to PDF, Excel, HTML, RTF, OpenXML, and Image.</p>"
                + "<h4>Reports</h4>"
                + "<p>This service exposes the following report files:</p>";
            c1APIIntroduction.Details = GetDetails();
            return c1APIIntroduction;
        }
    }
}