<!--
- Copyright 2019 Jason H House
-
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Gaps</title>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
    <link th:href="@{images/gaps.ico}" rel="shortcut icon"/>
    <link th:href="@{css/libraries/bootstrap.min.css}" rel="stylesheet"/>
    <link th:href="@{css/input.min.css}" rel="stylesheet">

    <!--Let browser know website is optimized for mobile-->
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
</head>

<body>

<div th:insert="fragments/header :: navBar"></div>

<div class="container">
    <h3 class="top-margin">Settings</h3>

    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link active" data-toggle="tab" href="#tmdb" id="tmdbTab">TMDB</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#plex" id="plexTab">Plex</a>
        </li>
        <li class="nav-item">
            <span class="badge badge-pill badge-primary" style="float:right;margin-bottom:-10px;">New</span>
            <a class="nav-link" data-toggle="tab" href="#schedule" id="scheduleTab">Schedule</a>
        </li>
        <li class="nav-item">
            <span class="badge badge-pill badge-primary" style="float:right;margin-bottom:-10px;">New</span>
            <a class="nav-link" data-toggle="tab" href="#notification" id="notificationTab">Notifications</a>
        </li>
        <li class="nav-item" hidden>
            <a class="nav-link disabled" data-ol-has-click-handler="" href="#folders" id="folderTab">Folders</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#network" id="networkTab">Network</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#advanced" id="advancedTab">Advanced</a>
        </li>
    </ul>
    <div class="tab-content top-margin" id="myTabContent">
        <div class="tab-pane fade show active top-margin" id="tmdb">
            <p>To use Gaps, you'll need a MovieDB api key. Navigate over to <a
                    href="https://www.themoviedb.org/settings/api" rel="noopener noreferrer" target="_blank">The Movie DB</a>,
                create an account, and make an API Key. Copy that key and paste it below.</p>

            <form class="needs-validation" id="tmdbConfiguration" novalidate th:object="${plexProperties}">
                <div class="form-group">
                    <label for="movieDbApiKey">Movie Database Api Key</label>
                    <div class="input-group">
                        <input class="form-control" id="movieDbApiKey" data-cy="movieDbApiKey" required th:field="*{movieDbApiKey}" type="text">
                        <div class="invalid-feedback" id="emptyTmdbKeyLabel">
                            Please enter a Movie Database Key.
                        </div>
                    </div>
                </div>
                <a class="btn btn-info" href="javascript:void(0)" id="testTmdbKey" onclick="testTmdbKey();"
                   type="button">Test</a>
                <a class="btn btn-primary" href="javascript:void(0)" id="saveTmdbKey" onclick="saveTmdbKey();"
                   type="button">Save</a>
            </form>

            <div class="spinner-border text-primary gaps-hide top-margin" id="tmdbSpinner" role="status">
                <span class="sr-only">Loading...</span>
            </div>

            <div class="alert alert-dismissible alert-danger gaps-hide top-margin" id="tmdbTestError">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Error!</h4>
                <p class="mb-0">Could not connect to The MovieDB. Check the key and logs and try again.</p>
            </div>

            <div class="alert alert-dismissible alert-success gaps-hide top-margin" id="tmdbTestSuccess">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Success!</h4>
                <p class="mb-0">Your MovieDB key is valid.</p>
            </div>

            <div class="alert alert-dismissible alert-danger gaps-hide top-margin" id="tmdbSaveError">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Error!</h4>
                <p class="mb-0">Could not save your MovieDB key. Check the logs and try again.</p>
            </div>

            <div class="alert alert-dismissible alert-success gaps-hide top-margin" id="tmdbSaveSuccess">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Success!</h4>
                <p class="mb-0">Your MovieDB key saved successfully.</p>
            </div>
        </div>
        <div class="tab-pane fade top-margin" id="plex">
            <form class="needs-validation" id="plexConfiguration" novalidate>
                <div class="form-group">
                    <label for="address">Plex IP Address</label>
                    <div class="input-group">
                        <input aria-describedby="addressHelp" class="form-control" id="address" required type="text">
                        <div class="invalid-feedback" id="emptyPlexAddressLabel">
                            Please enter a network address for Plex.
                        </div>
                    </div>
                    <small class="form-text text-muted" id="addressHelp">Use your network IP address. localhost and
                        127.0.0.1 will not work for Plex and Gaps in Docker. Use ifconfig/ipconfig to find that in a
                        terminal or command window.</small>
                </div>

                <div class="form-group">
                    <label for="port">Plex Port</label>
                    <div class="input-group">
                        <input aria-describedby="portHelp" class="form-control" id="port" required type="text"
                               value="32400">
                        <div class="invalid-feedback" id="emptyPlexPortLabel">
                            Please enter a port for Plex.
                        </div>
                    </div>
                    <small class="form-text text-muted" id="portHelp">Plex defaults to 32400. Update only if your
                        Plex
                        Server uses something
                        else.</small>
                </div>

                <div class="form-group">
                    <label for="plexToken">Plex Token</label>
                    <div class="input-group">
                        <input aria-describedby="tokenHelp" class="form-control" id="plexToken" required type="text">
                        <div class="invalid-feedback" id="emptyPlexTokenLabel">
                            Please enter a Plex token.
                        </div>
                    </div>
                    <small class="form-text text-muted" id="tokenHelp">You can learn how to find your Plex Token
                        <a href="https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/">here</a>
                        .</small>
                </div>

                <a class="btn btn-info" href="javascript:void(0)" id="testPlexServer" onclick="testPlexServer();"
                   type="button">Test</a>
                <a class="btn btn-primary" href="javascript:void(0)" id="addPlexServer" onclick="addPlexServer();"
                   type="button">Add</a>

                <div class="spinner-border text-primary gaps-hide top-margin" id="plexSpinner" role="status">
                    <span class="sr-only">Loading...</span>
                </div>

                <div class="alert alert-dismissible alert-danger gaps-hide top-margin" id="plexTestError">
                    <button class="close" data-hide="alert" type="button">&times;</button>
                    <h4 class="alert-heading">Error!</h4>
                    <p class="mb-0">Could not connect to Plex. Check the key and logs and try again.</p>
                </div>

                <div class="alert alert-dismissible alert-success gaps-hide top-margin" id="plexTestSuccess">
                    <button class="close" data-hide="alert" type="button">&times;</button>
                    <h4 class="alert-heading">Success!</h4>
                    <p class="mb-0">Your plex configuration is valid.</p>
                </div>

                <div class="alert alert-dismissible alert-danger gaps-hide top-margin" id="plexSaveError">
                    <button class="close" data-hide="alert" type="button">&times;</button>
                    <h4 class="alert-heading">Error!</h4>
                    <p class="mb-0">Could not save your Plex configuration. Check the logs and try again.</p>
                </div>

                <div class="alert alert-dismissible alert-success gaps-hide top-margin" id="plexSaveSuccess">
                    <button class="close" data-hide="alert" type="button">&times;</button>
                    <h4 class="alert-heading">Success!</h4>
                    <p class="mb-0">Your Plex configuration saved successfully.</p>
                </div>

                <div class="alert alert-dismissible alert-danger gaps-hide top-margin" id="plexDeleteError">
                    <button class="close" data-hide="alert" type="button">&times;</button>
                    <h4 class="alert-heading">Error!</h4>
                    <p class="mb-0">Could not delete your Plex configuration. Check the logs and try again.</p>
                </div>

                <div class="alert alert-dismissible alert-success gaps-hide top-margin" id="plexDeleteSuccess">
                    <button class="close" data-hide="alert" type="button">&times;</button>
                    <h4 class="alert-heading">Success!</h4>
                    <p class="mb-0">Your Plex configuration deleted successfully.</p>
                </div>

                <div class="alert alert-dismissible alert-warning gaps-hide top-margin" id="plexDuplicateError">
                    <button class="close" data-hide="alert" type="button">&times;</button>
                    <h4 class="alert-heading">Error!</h4>
                    <p class="mb-0">Could not add your Plex configuration. The Plex configuration already is added!</p>
                </div>
            </form>

            <h3 class="top-margin">Servers</h3>

            <div class="row top-margin" id="plexServers">
                <div class="col-lg-4" th:each="plexServer,status : *{plexProperties.plexServers}"
                     th:id="*{plexServer.machineIdentifier}">
                    <div class="top-margin">
                        <div class="card border-secondary mb-3" style="max-width: 20rem;">
                            <div class="card-header" th:text="${plexServer.getFriendlyName()}"></div>
                            <ul class="list-group list-group-flush">
                                <li class="list-group-item" th:each="plexLibrary,status : *{plexServer.plexLibraries}"
                                    th:text="${plexLibrary.getTitle()}"></li>
                            </ul>
                            <div class="card-body">
                                <a class="card-link" data-ol-has-click-handler=""
                                   href="javascript:void(0)"
                                   th:data-machineIdentifier="*{plexServer.machineIdentifier}"
                                   th:onclick="testExistingPlexServer(this.getAttribute('data-machineIdentifier'))">Test</a>
                                <a class="card-link" data-ol-has-click-handler=""
                                   href="javascript:void(0)"
                                   th:data-machineIdentifier="*{plexServer.machineIdentifier}"
                                   th:onclick="removePlexServer(this.getAttribute('data-machineIdentifier'))">Remove</a>
                            </div>
                            <div class="card-footer text-muted"
                                 th:text="'http://' + ${plexServer.getAddress()} + ':' + ${plexServer.getPort()}"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="tab-pane fade top-margin" id="schedule">
            <p>Gaps can be set to automatically run. This is through a cron job that will search for new Plex Libraries
                on each configured server, then search for all movies in each library, and produce a missing movie
                list.</p>

            <form class="needs-validation" id="scheduleConfiguration" novalidate th:object="${plexProperties}">
                <div class="form-group">
                    <label for="setSchedule">Set Schedule</label>
                    <select class="form-control" id="setSchedule">
                        <div th:each="schedule : ${schedules}" th:remove="tag">
                            <option th:value="${schedule.id}" th:text="${schedule.message}" th:selected="${plexProperties?.schedule?.getId().equals(schedule.id)?: false}"></option>
                        </div>
                    </select>
                </div>
                <div class="form-group">
                    <label for="scheduleEnabled">Schedule Agent</label>
                    <select class="form-control" id="scheduleEnabled">
                        <option value="false">Disabled</option>
                        <option value="true" th:selected="${plexProperties?.schedule?.getEnabled() ?: false}">Enabled</option>
                    </select>
                </div>
                <div class="form-group">
                    <a class="btn btn-primary" href="javascript:void(0)" id="saveSchedule" onclick="saveSchedule();"
                       type="button">Save</a>
                </div>
            </form>

            <div class="spinner-border text-primary gaps-hide top-margin" id="scheduleSpinner" role="status">
                <span class="sr-only">Loading...</span>
            </div>

            <div class="alert alert-dismissible alert-danger gaps-hide top-margin" id="scheduleSaveError">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Error!</h4>
                <p class="mb-0">Could not save your schedule. Check the logs and try again.</p>
            </div>

            <div class="alert alert-dismissible alert-success gaps-hide top-margin" id="scheduleSaveSuccess">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Success!</h4>
                <p class="mb-0">Your schedule saved successfully.</p>
            </div>

        </div>
        <div class="tab-pane fade top-margin" id="notification">
            <div th:insert="fragments/notifications :: discord"></div>
            <hr class="thin">
            <div th:insert="fragments/notifications :: email"></div>
            <hr class="thin">
            <div th:insert="fragments/notifications :: gotify"></div>
            <hr class="thin">
            <div th:insert="fragments/notifications :: push-bullet"></div>
            <hr class="thin">
            <div th:insert="fragments/notifications :: push-over"></div>
            <hr class="thin">
            <div th:insert="fragments/notifications :: slack"></div>
            <hr class="thin">
            <div th:insert="fragments/notifications :: telegram"></div>
        </div>
        <div class="tab-pane fade top-margin" id="folders">
            <p>folders</p>
        </div>
        <div class="tab-pane fade top-margin" id="network">

            <form class="needs-validation" id="networkConfiguration" novalidate>
                <h4 class="text-primary">Plex</h4>
                <div class="form-group">
                    <label for="port">Plex Connection Timeout</label>
                    <div class="input-group">
                        <input aria-describedby="plexConnectionTimeoutHelp" class="form-control"
                               id="plexConnectionTimeout" required type="text"
                               value="2500">
                        <div class="invalid-feedback" id="emptyPlexConnectionTimeout">
                            Please enter a valid time.
                        </div>
                    </div>
                    <small class="form-text text-muted" id="plexConnectionTimeoutHelp">Default connect timeout (in
                        milliseconds).</small>
                </div>
                <div class="form-group">
                    <label for="port">Plex Connection Timeout</label>
                    <div class="input-group">
                        <input aria-describedby="plexReaTimeoutHeldp" class="form-control"
                               id="plexReadTimeout" required type="text"
                               value="2500">
                        <div class="invalid-feedback" id="emptyPlexReadTimeout">
                            Please enter a valid time.
                        </div>
                    </div>
                    <small class="form-text text-muted" id="plexReadTimeoutHelp">Default read timeout (in
                        milliseconds).</small>
                </div>
                <div class="form-group">
                    <label for="port">Plex Write Timeout</label>
                    <div class="input-group">
                        <input aria-describedby="plexWriteTimeoutHelp" class="form-control"
                               id="plexWriteTimeout" required type="text"
                               value="2500">
                        <div class="invalid-feedback" id="emptyPlexWriteTimeout">
                            Please enter a valid time.
                        </div>
                    </div>
                    <small class="form-text text-muted" id="plexWriteTimeoutHelp">Default write timeout (in
                        milliseconds).</small>
                </div>

                <a class="btn btn-primary" href="javascript:void(0)" id="updateNetwork" onclick="updateNetwork();"
                   type="button">Update</a>

            </form>
        </div>
        <div class="tab-pane fade top-margin" id="advanced">

            <form class="needs-validation" id="advancedConfiguration" novalidate>
                <div class="form-group">
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" id="confirmDeleteAll"
                               onchange="setDeleteAllEnabledOrDisabled();">
                        <label class="form-check-label" for="confirmDeleteAll">Delete All Stored Data</label>
                    </div>
                </div>
                <button class="btn btn-primary" id="deleteAll" onclick="nuke();" disabled type="button">Delete</button>
            </form>

            <div class="alert alert-dismissible alert-danger gaps-hide top-margin" id="deleteAllError">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Error!</h4>
                <p class="mb-0">Could not delete stored data. Check the logs and try again.</p>
            </div>

            <div class="alert alert-dismissible alert-success gaps-hide top-margin" id="deleteAllSuccess">
                <button class="close" data-hide="alert" type="button">&times;</button>
                <h4 class="alert-heading">Success!</h4>
                <p class="mb-0">Deleting stored data successful.</p>
            </div>
        </div>
    </div>
</div>

<div th:insert="fragments/common :: contextPath"></div>

<script id="plexServerCard" type="text/x-handlebars-template">
    <div id="{{machineIdentifier}}" class="col-lg-4">
        <div class="top-margin">
            <div class="card border-secondary mb-3" style="max-width: 20rem;">
                <div data-cy="{{friendlyName}}" class="card-header">{{friendlyName}}</div>
                <ul class="list-group list-group-flush">
                    {{#each plexLibraries}}
                    <li data-cy="{{title}}" class="list-group-item">{{title}}</li>
                    {{/each}}
                </ul>
                <div class="card-body">
                    <a id="testExisting" href="javascript:void(0)"
                       onclick="testExistingPlexServer('{{machineIdentifier}}')"
                       class="card-link" data-ol-has-click-handler="">Test</a>
                    <a id="removeExisting" href="javascript:void(0)" onclick="removePlexServer('{{machineIdentifier}}')"
                       class="card-link"
                       data-ol-has-click-handler="">Remove</a>
                </div>
                <div class="card-footer text-muted">http://{{address}}:{{port}}</div>
            </div>
        </div>
    </div>
</script>

<script th:src="@{/js/libraries/jquery-3.4.1.min.js}" type="text/javascript"></script>
<script th:src="@{/js/libraries/bootstrap.bundle.min.js}" type="text/javascript"></script>
<script th:src="@{/js/libraries/sockjs-1.4.0.min.js}" type="text/javascript"></script>
<script th:src="@{/js/libraries/stomp-2.3.3.min.js}" type="text/javascript"></script>
<script th:src="@{/js/libraries/handlebars-v4.7.6.min.js}" type="text/javascript"></script>
<script th:src="@{/js/page/configuration.min.js}" type="module"></script>
</body>
</html>