@using NzbDrone.Web.Helpers
@model NzbDrone.Web.Models.IndexerSettingsModel

@{
    Layout = "_SettingsLayout.cshtml";
}

<style>
    .indexerStatusContainer {
        margin-left: 14px;
    }
</style>

<div class="indexerStatusContainer">   
    @Html.CheckBox("nzbsRusStatus", @Model.NzbsRUsEnabled, new { @class = "indexerStatusButton" })
    <label for="nzbsRusStatus">NZBsRus</label>
    
    @Html.CheckBox("newznabStatus", @Model.NewznabEnabled, new { @class = "indexerStatusButton" })
    <label for="newznabStatus">Newznab</label>

    @Html.CheckBox("womblesStatus", @Model.WomblesEnabled, new { @class = "indexerStatusButton" })
    <label for="womblesStatus">Womble's Index</label>

    @Html.CheckBox("fileSharingTalkStatus", @Model.FileSharingTalkEnabled, new { @class = "indexerStatusButton" })
    <label for="fileSharingTalkStatus">File Sharing Talk</label>

    @Html.CheckBox("nzbIndexStatus", @Model.NzbIndexEnabled, new { @class = "indexerStatusButton" })
    <label for="nzbIndexStatus">NzbIndex</label>

    @Html.CheckBox("nzbClubStatus", @Model.NzbClubEnabled, new { @class = "indexerStatusButton" })
    <label for="nzbClubStatus">Nzb Club</label>
    
    @Html.CheckBox("omgwtfnzbsStatus", @Model.OmgwtfnzbsEnabled, new { @class = "indexerStatusButton" })
    <label for="omgwtfnzbsStatus">omgwtfnzbs</label>
    
    @Html.CheckBox("nzbxStatus", @Model.NzbxEnabled, new { @class = "indexerStatusButton" })
    <label for="nzbxStatus">nzbx</label>
</div>
<div id="stylized">
    @using (Html.BeginForm("SaveIndexers", "Settings", FormMethod.Post, new { id = "IndexersForm", name = "IndexersForm", @class = "settingsForm" }))
    {     
        <div class="jquery-accordion jquery-accordion-collapse-all">
            <h3><a href="#">NZBsRus</a></h3>
            <div class="indexerPanel">
                <div class="warningBox">NZBsRus does not support backlog searching</div>
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.NzbsRUsEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.NzbsRUsEnabled, new { @class = "inputClass checkClass enabledCheck" })
                <label class="labelClass">@Html.LabelFor(m => m.NzbsrusUId)
                    <span class="small">@Html.DescriptionFor(m => m.NzbsrusUId)</span>
                    <span class="small">@Html.ValidationMessageFor(m => m.NzbsrusUId)</span>
                </label>
                @Html.TextBoxFor(m => m.NzbsrusUId, new { @class = "inputClass" })
                <label class="labelClass">@Html.LabelFor(m => m.NzbsrusHash)
                    <span class="small">@Html.DescriptionFor(m => m.NzbsrusHash)</span>
                    <span class="small">@Html.ValidationMessageFor(m => m.NzbsrusHash)</span>
                </label>
                @Html.TextBoxFor(m => m.NzbsrusHash, new { @class = "inputClass" })
            </div>

            <h3><a href="#">Newznab</a></h3>
            <div class="indexerPanel">
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.NewznabEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.NewznabEnabled, new { @class = "inputClass checkClass enabledCheck" })
                <br />
                <br />
                <p>
                </p>
                <a id="addNewznab" href="@Url.Action("AddNewznabProvider", "Settings")">
                    <i class="icon-plus add-newznab"></i>
                    Add Newznab Provider</a>
                <div id="newznabProviders">
                    @foreach (var provider in Model.NewznabDefinitions)
                    {
                        Html.RenderAction("GetNewznabProviderView", provider);
                    }
                </div>
            </div>

            <h3><a href="#">Womble's Index</a></h3>
            <div class="indexerPanel">
                <div class="warningBox">Womble's Index does not support backlog searching</div>
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.WomblesEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.WomblesEnabled, new { @class = "inputClass checkClass enabledCheck" })
            </div>

            <h3><a href="#">File Sharing Talk</a></h3>
            <div class="indexerPanel">
                <div class="warningBox">File Sharing Talk does not support backlog searching</div>
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.FileSharingTalkEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.FileSharingTalkEnabled, new { @class = "inputClass checkClass enabledCheck" })
                <label class="labelClass">@Html.LabelFor(m => m.FileSharingTalkUid)
                    <span class="small">@Html.DescriptionFor(m => m.FileSharingTalkUid)</span>
                    <span class="small">@Html.ValidationMessageFor(m => m.FileSharingTalkUid)</span>
                </label>
                @Html.TextBoxFor(m => m.FileSharingTalkUid, new { @class = "inputClass" })
                <label class="labelClass">@Html.LabelFor(m => m.FileSharingTalkSecret)
                    <span class="small">@Html.DescriptionFor(m => m.FileSharingTalkSecret)</span>
                    <span class="small">@Html.ValidationMessageFor(m => m.FileSharingTalkSecret)</span>
                </label>
                @Html.TextBoxFor(m => m.FileSharingTalkSecret, new { @class = "inputClass" })
            </div>

            <h3><a href="#">NzbIndex</a></h3>
            <div class="indexerPanel">
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.NzbIndexEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.NzbIndexEnabled, new { @class = "inputClass checkClass enabledCheck" })
            </div>

            <h3><a href="#">NzbClub</a></h3>
            <div class="indexerPanel">
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.NzbClubEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.NzbClubEnabled, new { @class = "inputClass checkClass enabledCheck" })
            </div>
            
            <h3><a href="#">omgwtfnzbs</a></h3>
            <div class="indexerPanel">
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.OmgwtfnzbsEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.OmgwtfnzbsEnabled, new { @class = "inputClass checkClass enabledCheck" })
                
                <label class="labelClass">@Html.LabelFor(m => m.OmgwtfnzbsUsername)
                    <span class="small">@Html.DescriptionFor(m => m.OmgwtfnzbsUsername)</span>
                    <span class="small">@Html.ValidationMessageFor(m => m.OmgwtfnzbsUsername)</span>
                </label>
                @Html.TextBoxFor(m => m.OmgwtfnzbsUsername, new { @class = "inputClass" })
                <label class="labelClass">@Html.LabelFor(m => m.OmgwtfnzbsApiKey)
                    <span class="small">@Html.DescriptionFor(m => m.OmgwtfnzbsApiKey)</span>
                    <span class="small">@Html.ValidationMessageFor(m => m.OmgwtfnzbsApiKey)</span>
                </label>
                @Html.TextBoxFor(m => m.OmgwtfnzbsApiKey, new { @class = "inputClass" })
            </div>
            
            <h3><a href="#">nzbx</a></h3>
            <div class="indexerPanel">
                <label class="labelClass">
                    Enable <span class="small">@Html.DescriptionFor(m => m.NzbxEnabled)</span>
                </label>
                @Html.CheckBoxFor(m => m.NzbxEnabled, new { @class = "inputClass checkClass enabledCheck" })
            </div>
        </div>
        
        <div class="indexer-global-settings">
            @Html.ValidationSummary(true, "Unable to save your settings. Please correct the errors and try again.")
            <label class="labelClass">@Html.LabelFor(m => m.Retention)
                <span class="small">@Html.DescriptionFor(m => m.Retention)</span>
                <span class="small">@Html.ValidationMessageFor(m => m.Retention)</span>
            </label>
            @Html.TextBoxFor(m => m.Retention, new { @class = "inputClass" })
            
            <label class="labelClass">@Html.LabelFor(m => m.RssSyncInterval)
                <span class="small">@Html.DescriptionFor(m => m.RssSyncInterval)</span>
                <span class="small">@Html.ValidationMessageFor(m => m.RssSyncInterval)</span>
            </label>
            @Html.TextBoxFor(m => m.RssSyncInterval, new { @class = "inputClass" })
        </div>
        
        <button type="submit" class="save_button" disabled="disabled">Save</button>
    }
</div>

@section Scripts
{
    <script type="text/javascript">
    $(document).ready(function () {
        //Allow unobstrusive validation of the AJAX loaded form
        $.validator.unobtrusive.parse("#IndexersForm");
        
        //Make the buttons
        $('.indexerStatusButton').button();

        //Validator Settings
        var settings = $.data($('#IndexersForm')[0], 'validator').settings;
        settings.ignore = [];
        settings.focusInvalid = false;
        settings.onfocusout = function (element) { $(element).valid(); };

        var oldHighlight = settings.highlight;
        var oldUnhighlight = settings.unhighlight;

        settings.highlight = function (element, errorClass, validClass) {
            oldHighlight(element, errorClass, validClass);
            $(element).parents('div.ui-accordion-content').prev('h3.ui-accordion-header').addClass('validation-error');
        };
        settings.unhighlight = function (element, errorClass, validClass) {
            oldUnhighlight(element, errorClass, validClass);

            var container = $(element).parents('div.ui-accordion-content');
            if ($(container).children('.' + errorClass).length == 0)
                $(container).prev('h3.ui-accordion-header').removeClass('validation-error');
        };
    });

    $(document).on('change', '.enabledCheck', function () {
        var id = $(this).prop('id');
        var checked = $(this).prop('checked');

        if (id == 'NzbsRUsEnabled')
            $('#nzbsRusStatus').prop('checked', checked);

        if (id == 'NewznabEnabled')
            $('#newznabStatus').prop('checked', checked);
        
        if (id == 'WomblesEnabled')
            $('#womblesStatus').prop('checked', checked);
        
        if (id == 'FileSharingTalkEnabled')
            $('#fileSharingTalkStatus').prop('checked', checked);
        
        if (id == 'NzbIndexEnabled')
            $('#nzbIndexStatus').prop('checked', checked);
        
        if (id == 'NzbClubEnabled')
            $('#nzbClubStatus').prop('checked', checked);
        
        if (id == 'OmgwtfnzbsEnabled')
            $('#omgwtfnzbsStatus').prop('checked', checked);
        
        if (id == 'NzbxEnabled')
            $('#nzbxStatus').prop('checked', checked);

        $('.indexerStatusButton').button("refresh");
        reValidate();
    });

    $(document).on('change', '.indexerStatusButton', function () {
        var id = $(this).prop('id');
        var checked = $(this).prop('checked');

        if (id == 'nzbsRusStatus')
            $('#NzbsRUsEnabled').prop('checked', checked);

        if (id == 'newznabStatus')
            $('#NewznabEnabled').prop('checked', checked);
        
        if (id == 'womblesStatus')
            $('#WomblesEnabled').prop('checked', checked);
        
        if (id == 'fileSharingTalkStatus')
            $('#FileSharingTalkEnabled').prop('checked', checked);
        
        if (id == 'nzbIndexStatus')
            $('#NzbIndexEnabled').prop('checked', checked);
        
        if (id == 'nzbClubStatus')
            $('#NzbClubEnabled').prop('checked', checked);
        
        if (id == 'omgwtfnzbsStatus')
            $('#OmgwtfnzbsEnabled').prop('checked', checked);
        
        if (id == 'nzbxStatus')
            $('#NzbxEnabled').prop('checked', checked);

        reValidate();
    });

    function reValidate() {
        $("#IndexersForm").validate().form();

        var container = $('div.ui-accordion-content');
        if ($(container).children('.input-validation-error').length == 0)
            $(container).prev('h3.ui-accordion-header').removeClass('validation-error');
    }

    //Newznab
    $("#addNewznab").on('click', function (event) {
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {
                $("#newznabProviders").append(html);
            }
        });
        event.preventDefault();
        //event.stopPropagation();
    });

    var deleteNewznabProviderUrl = '@Url.Action("DeleteNewznabProvider", "Settings")';

    function deleteProvider(id) {
        $.ajax({
            type: "POST",
            url: deleteNewznabProviderUrl,
            data: jQuery.param({ providerId: id }),
            error: function (req, status, error) {
                alert("Sorry! We could not delete your Provider at this time. " + error);
            },
            success: function (data, textStatus, jqXHR) {
                $("#provider_" + id).remove();
            }
        });
    }

    function getProviderId(obj) {
        var parentProviderSection = $(obj).parents('.providerSection');
        return parentProviderSection.children('.newznabProviderId').val();
    }

    $(".providerName_textbox").on('keyup', function () {
        var value = $(this).val();
        var profileId = getProviderId(this);
        $("#title_" + profileId).text(value);
    }).keyup();
  </script>
}
