@using OurUmbraco.Release
@inherits UmbracoTemplatePage
@{
    Layout = "~/Views/Master.cshtml";

    var downloads = Model.Content.Children().Where(x => x.ContentType.Alias == "ReleaseDownload").ToArray();
    if (Request.Params["fromdownload"] != null)
    {
        if (downloads.Any())
        {
            var downloadIds = new List<int>();
            foreach (var item in downloads)
            {
                downloadIds.Add(item.Id);
            }
            Response.Redirect("/ReleaseDownload?id=" + downloadIds.First(), false);
        }
    }

    var currentPage = UmbracoContext.Current.PublishedContentRequest.PublishedContent;
    var allReleases = Release.GetAllReleaseFromFile();
    var release = allReleases.First(x => x.version == currentPage.Name);
    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");


    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var releaseContent = umbracoHelper.TypedContent(umbracoHelper.UmbracoContext.PageId);
    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="has-sidebar release">
    <div class="page-content">
        <div class="sidebar-area">
            <div class="sidebar-content">
                <div class="content-wrapper">

                    <nav>
                        <ul class="level-1">
                            @foreach (var currentRelease in allReleases.Where(x => x.released && x.currentRelease))
                            {

                                <li>
                                    <a class="active" href="/contribute/releases/@(currentRelease.version.Replace(".", string.Empty))"><h3>Latest Release - v@(currentRelease.version)</h3></a>
                                </li>
                            }

                            <li>
                                <a href="/contribute/progress"><h3>In Progress</h3></a>
                            </li>
                            <li>
                                <a href="/contribute/releases"><h3>All Releases</h3></a>
                                <ul class="level-2">
                                    <li>
                                        <a href=""><h4>Umbraco 7</h4></a>
                                    </li>
                                    <li>
                                        <a href=""><h4>Umbraco 6</h4></a>
                                    </li>
                                    <li>
                                        <a href=""><h4>Umbraco 4</h4></a>
                                    </li>
                                </ul>
                            </li>
                        </ul>
                    </nav>
                </div><!-- .content-wrapper -->
            </div><!-- .sidebar-content -->
        </div>
        <div class="main-area">
            <div class="main-content">
                <div class="content-wrapper">
                <div>
                    @Html.Action("Render", "Breadcrumb", new { linkToCurrent = true })
                </div>
                    <style>
                        .release-page .explain h2 {
                            margin-top: 12px;
                            font-size: 2em;
                            margin-bottom: .3rem;
                            color: rgba(0,0,0,.8);
                        }

                        .release-page .dot {
                            border: none;
                        }

                            .release-page .dot.big {
                                position: relative;
                                text-indent: 0;
                                margin: 15px auto 0;
                            }

                        .release-page .chart {
                            height: 60px;
                            width: 60px;
                            top: initial;
                            left: initial;
                            text-align: center;
                        }

                            .release-page .chart p {
                                vertical-align: middle;
                                line-height: 3.7rem;
                                display: inline-block;
                            }

                            .release-page .chart canvas {
                                top: 0px;
                                left: 0px;
                            }

                        .statuses ul {
                            list-style-type: none;
                        }

                            .statuses ul li {
                                position: relative;
                                margin-bottom: 10px;
                                padding-left: 10px;
                            }

                        .statuses li:before {
                            position: absolute;
                            top: -4px;
                            left: -25px;
                            font-size: 1.5em;
                        }
                    </style>

                    <div class="row explain">
                        <div class="col-sm-2">
                            <span class="dot big progress">
                                <div class="chart" data-percent="percentComplete">
                                    <p>@(percentComplete)%</p>
                                </div>
                            </span>
                        </div>
                        <div class="col-sm-10">
                            <h2>v@(Model.Content.Name)</h2>
                            <small>Status: <span>@(release.released ? "Released" : "In Progress")</span></small>
                            @if (release.released == false)
                            {
                                <h3>
                                    <span>@(percentComplete)% complete</span>
                                    <span>@(100 - percentComplete)% in progress</span>
                                </h3>
                                <h3>Target release date: <span style="color:#5B5B5B">@releaseDate</span></h3>
                            }

                            @if (Model.Content.HasValue("releaseNotes"))
                            {
                                <small><p>@Html.Raw(Model.Content.GetPropertyValue<string>("releaseNotes"))</p></small>
                            }
                        </div>
                    </div>
                    <div>
                        @{
                            var countIsOdd = ((downloads.Count() & 1) != 0) ? true : false;
                            if (downloads.Any())
                            {

                                <div class="get-release row">
                                    @foreach (var item in downloads)
                                    {
                                        var background = item.IsFirst() ? "/images/releases/download.png" : "/media/2483095/download.png";
                                        var width = (item.IsLast() && countIsOdd) ? 895 : 380;
                                        var numberOfDownloads = item.GetPropertyValue<int>("numberOfDownloads");
                                        numberOfDownloads = numberOfDownloads + OurUmbraco.Our.Utils.GetReleaseDownloadCount(item.Id);
                                        if (item.Name.Contains("WebPI") == false)
                                        {
                                            <div class="release @(item.IsFirst() == false ? "bin" : "zip") col-sm-1">
                                                <i class="icon-Download-alt"></i>
                                            </div>
                                            <div class="release @(item.IsFirst() == false ? "bin" : "zip") col-md-5">

                                                <h3><a href="/ReleaseDownload?id=@item.Id" onclick="_gaq.push(['_trackEvent', 'Release', 'Download', '@item.Name']);">@item.Name</a></h3>
                                                @if (item.IsFirst())
                                                {
                                                    <p>This is the main Umbraco download, generally you won't need anything else.</p>
                                                }
                                                <small>Downloaded @numberOfDownloads times - uploaded @(item.GetPropertyValue<DateTime>("UploadDate").ToString("D"))</small>
                                            </div>
                                        }
                                        else
                                        {
                                            <div style="display: none;"><a href="/ReleaseDownload?id=@item.Id" onclick="_gaq.push(['_trackEvent', 'Release', 'Download', '@item.Name']);">@item.Name</a></div>
                                        }
                                    }
                                </div>
                            }
                        }
                        <div class="row markdown-syntax">

                            <div class="col-md-5">

                                @if (release.releaseDescription != string.Empty && currentPage.HasValue("bodyText") == false)
                                {
                                    <span>
                                        <h2>Summary</h2>
                                        <p><span>@Html.Raw(release.releaseDescription)</span></p>
                                    </span>
                                }

                                @if (Model.Content.HasValue("bodyText"))
                                {
                                    <h2>Release notes</h2>
                                    @Html.Raw(Model.Content.GetPropertyValue<string>("bodyText"))
                                }

                                <h2>Related changes from the issue tracker</h2>
                                <div class="panel-wrapper">
                                    <div class="panel-left">
                                        <!-- Columns for statuses -->
                                        <div class="statuses group">
                                            <div class="status features">
                                                <h3>Features</h3>
                                                @{
                                                    var features = release.issues.Where(x => x.type.ToLowerInvariant().Contains("feature")).ToList();
                                                    if (features.Any())
                                                    {
                                                        foreach (var issue in features)
                                                        {
                                                            <ul>
                                                                <li class="@issue.StateIcon()" title="@issue.state">
                                                                    <a href="http://issues.umbraco.org/issue/@issue.id" target="_blank">@issue.id - @issue.title</a>
                                                                </li>
                                                            </ul>
                                                        }
                                                    }
                                                    else
                                                    {
                                                        <p>No items to display</p>
                                                    }
                                                }
                                            </div>
                                            <div class="status breaking">
                                                <h3>Breaking Changes</h3>
                                                @{
                                                    var breaking = release.issues.Where(x => x.breaking).ToList();
                                                    if (breaking.Any())
                                                    {
                                                        foreach (var issue in breaking)
                                                        {
                                                            <ul>
                                                                <li class="@issue.StateIcon()" title="@issue.state">
                                                                    <a href="http://issues.umbraco.org/issue/@issue.id" target="_blank">@issue.id - @issue.title</a>
                                                                </li>
                                                            </ul>
                                                        }
                                                    }
                                                    else
                                                    {
                                                        <p>No items to display</p>
                                                    }
                                                }
                                            </div>
                                            <div class="status issues">
                                                <h3>Issues & Tasks</h3>
                                                @{
                                                    var issues = release.issues.Where(x => x.breaking == false && x.type.ToLowerInvariant().Contains("feature") == false).ToList();
                                                    if (issues.Any())
                                                    {
                                                        foreach (var issue in issues)
                                                        {

                                                            <ul>
                                                                <li class="@issue.StateIcon()" title="@issue.state">
                                                                    <a href="http://issues.umbraco.org/issue/@issue.id" target="_blank">@issue.id - @issue.title</a>
                                                                </li>
                                                            </ul>
                                                        }
                                                    }
                                                    else
                                                    {
                                                        <p>No items to display</p>
                                                    }
                                                }
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-5 col-md-offset-2">

                                <h2>Getting started</h2>
                                <p>Make sure to give the <a href="http://our.umbraco.org/documentation/Installation/">installation instructions a read</a>.</p>
                                <p>
                                    Check the <strong>free</strong> <a href="http://umbraco.tv/videos/umbraco-v7/implementor/">foundation videos</a> on how to get started building Umbraco sites. They're available from:<br />
                                    <ul>
                                        <li><a href="http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/installation/">Installation</a></li>
                                        <li><a href="http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/document-types/">Document Types</a></li>
                                        <li><a href="http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/templating/">Templating</a></li>
                                        <li><a href="http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/stylesheets-and-javascript/">Stylesheets and Javascript</a></li>
                                    </ul>
                                    <h2>World's best community</h2>
                                <p>You'll find our über active community working hard to help each other at Our Umbraco: <a href="http://our.umbraco.org">http://our.umbraco.org</a></p>
                                <h2>Upgrading from older versions</h2>
                                <p>Make sure to read the <a href="http://our.umbraco.org/documentation/Installation/Upgrading/">upgrade instructions</a> before you attempt an upgrade.</p>


                                <h2>Found a bug?</h2>
                                <p>We're happy to help you with any problems you encounter, so please report them in <a href="http://issues.umbraco.org/issues/U4">the issue tracker</a> and use the “Affected versions” field to set it to @Model.Content.Name. <br />Do a search first though, if the issue already exists, give it an extra vote.</p>

                                @if (Model.Content.HasValue("linkToChangeset"))
                                {
                                    <h2>Source code</h2>
                                    <p>The source for <a href="@(Model.Content.GetPropertyValue<string>("linkToChangeset"))">this specific release is available for download from the repository</a> (click the "ZIP" button).</p>
                                }

                                @if (Model.Content.GetPropertyValue<bool>("hasNuGet"))
                                {
                                    <h2>Also available on NuGet</h2>

                                    <div class="nuget-badge">
                                        <a href="https://nuget.org/packages/UmbracoCms/@Model.Content.Name">
                                            <p><code>PM&gt; Install-Package UmbracoCms -Version @Model.Content.Name</code></p>
                                        </a>
                                    </div>
                                }
                            </div><!-- col-md-5 col-md-offset-2 -->
                        </div><!-- .row .markdown-syntax -->
                    </div>
                </div><!-- .content-wrapper -->
            </div><!-- .main-content -->
        </div><!-- .main-area -->
    </div><!-- .page-content -->
</div><!-- has-sidebar -->

@section EndScripts {
    <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>
}