<%@ Master Language="C#" MasterPageFile="~/masterpages/Master.master" AutoEventWireup="true" %>
<%@ Import Namespace="OurUmbraco.Release" %>
<%@ Import Namespace="Umbraco.Web" %>
<%@ Import Namespace="Umbraco.Core.Models" %>
<asp:Content ContentPlaceHolderID="Head" runat="server">
    <% if (Request.Url.AbsolutePath.ToLowerInvariant() == "/roadmap/" || Request.Url.AbsolutePath.ToLowerInvariant() == "/roadmap")
        {
            var newUrl = Request.Url.AbsoluteUri.Replace("/roadmap", "/contribute/roadmap");
            Response.Redirect(newUrl, true);
        } 
    %>
</asp:Content>

<asp:Content ContentPlaceHolderID="Main" runat="server">
    <style>
        .chart canvas {
            top: -3px;
            position: absolute;
            left: -3px;
        }
    </style>
    <div class="subpage wrapper">
        <div>
            <umbraco:Macro Alias="Breadcrumb" runat="server"></umbraco:Macro>
        </div>
        <div class="markdown-syntax">
            <h1>
                <umbraco:Item Field="alternativeHeadline" useIfEmpty="pageName" runat="server"></umbraco:Item>
            </h1>

            <umbraco:Item Field="bodyText" runat="server"></umbraco:Item>
            <h2>Planned Releases:</h2>

        </div>
        <% var roadmap = Roadmap.GetRoadmapReleasesFromFile(); %>
        <%            
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(UmbracoContext.Current);
            var currentPage = umbracoHelper.TypedContent(umbracoHelper.UmbracoContext.PageId);
            var releaseLanding = currentPage.Parent.Children().FirstOrDefault(x => x.ContentType.Alias == "ReleaseLanding");
        %>
        <% foreach (var release in roadmap.OrderBy(x => x.version))
            {
                var completedIssues = release.issues.Count(x => x.state == "Fixed");
                var totalIssues = release.issues.Count();
                var percentComplete = Math.Round(((double)100 / totalIssues) * completedIssues);
                var releaseDateParsed = DateTime.Now;
                if (release.releaseDate != string.Empty)
                {
                    releaseDateParsed = DateTime.Parse(release.releaseDate.Substring(3, 2) + "-" + release.releaseDate.Substring(0, 2) + "-" + release.releaseDate.Substring(6));
                }

                var releaseDate = release.releaseDate == "" ? "Not yet determined" : releaseDateParsed.ToString("dddd, MMMM d yyyy");
                IPublishedContent releaseContent = null;
                if (releaseLanding != null)
                {
                    releaseContent = releaseLanding.Children().FirstOrDefault(x => x.Name.ToLowerInvariant() == release.version.ToLowerInvariant());
                    if (releaseContent != null)
                    {
                        var overrideDescription = releaseContent.GetPropertyValue<bool>("overrideYouTrackDescription");
                        if (overrideDescription)
                        {
                            release.releaseDescription = releaseContent.GetPropertyValue<string>("bodyText");
                        }
                        var overrideReleaseDate = releaseContent.GetPropertyValue<string>("overrideYouTrackReleaseDate");
                        if (string.IsNullOrWhiteSpace(overrideReleaseDate) == false)
                        {
                            releaseDate = overrideReleaseDate;
                        }
                                        
                    }
                }

        %>
        <div class="releaseListing">
            <div class="plannedreleases">
                
                <div class="row" style="margin-top: 50px; margin-bottom: 50px;">
                    <div class="col-xs-3">
                        <span class="dot big progress">
                            <div class="chart" data-percent="<%= percentComplete %>">
                                <div class="percentage"><%= percentComplete %>%</div>
                            </div>
                            <span class="line v-line"></span>
                            <span class="line h-line"></span>
                        </span>

                        <span class="dot small">
                            <span class="line v-line"></span>
                            <span class="line h-line"></span>
                        </span>

                        <span class="dot small">
                            <span class="line h-line"></span>
                        </span>
                    </div>

                    <div class="col-xs-9">
                        <div class="row explain">
                            <div class="col-xs-12">
                                <h4 class="text-right"><a href="/contribute/releases/<%= release.version.Replace(".","") %>">v<%= release.version %></a></h4>
                                <small><span><%= release.releaseDescription %></span></small>
                            </div>
                            <div class="col-xs-6">
                                <div class="changes">
                                    <small>Features: <span><%= release.issues.Count(x => x.type.ToLowerInvariant().Contains("feature")) %></span></small>
                                    <small>Issues: <%= release.issues.Count(x => x.type.ToLowerInvariant().Contains("feature") == false && x.breaking == false) %><span></span></small>
                                    <small>Breaking changes: <span><%= release.issues.Count(x => x.breaking) %></span></small>
                                </div>
                            </div>
                            <div class="col-xs-6">
                                <div class="changes">
                                    <small>Release date:</small>
                                    <small><%= releaseDate %></small>
                                </div>
                            </div>

                            <div class="col-xs-12" style="min-height: initial">
                                <h1 style="margin-top: 11px; margin-bottom: 30px; font-size: 1.4rem;">Planned features:</h1>
                            </div>
                            <div class="issues">
                                <% foreach(var issue in release.issues.Where(x => x.type.ToLowerInvariant().Contains("feature"))) { %>
                                    <div class="col-sm-6" style="min-height: initial">
                                        <div class="<%= issue.state.Replace(" ", string.Empty) %>">
                                            <a href="http://issues.umbraco.org/issue/<%= issue.id %>" target="_blank"><small><%= issue.id %> <%= issue.title %></small></a>
                                        </div>
                                    </div>
                                <% } %>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
        <% } %>
    </div>
</asp:Content>

<asp:Content ContentPlaceHolderID="EndScripts" runat="server">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/easy-pie-chart/2.1.4/jquery.easypiechart.min.js"></script>    
    <script type="text/javascript">
        setTimeout(function () {
            $('.chart').easyPieChart({
                size: 60,
                barColor: '#569327',
                scaleColor: false,
                trackColor: '#daf0c9'
            });
        }, 300);
    </script>
</asp:Content>
