{#-
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
-#}
{% extends 'forgeimporters:templates/project_base.html' %}


{% block project_fields %}
    <div class="grid-16" style="clear: left">
        <p>Paste your Github URL to auto-fill your repository information</p>
    </div>
     <div class="grid-7" style="clear:left">
        <label>GitHub Repository URL
            <span class="tooltip" title="A valid Github repository URL">{{ g.icons['help'].render() }}</span>
        </label>
    </div>
     <div class="grid-10">
        <input id="project_url" name="project_url" value="{{request.validation.values['project_url']}}" />
        <div id="project_url_error" class="error{% if not request.validation.errors['project_url'] %} hidden{% endif %}">
            {{request.validation.errors['project_url']}}
        </div>
    </div>
    <div class="grid-10 field-divider">
        <label>OR</label>
    </div>
    <div class="grid-16" style="clear: left">
    <p>Manually enter your Github repository information</p>
    </div>
    <div class="grid-7" style="clear:left">
        <label>
            Github User / Organization
            <span class="tooltip" title="Your Github user or organization Name.">{{ g.icons['help'].render() }}</span>
        </label>
    </div>
     <div class="grid-10">
        <input id="user_name" name="user_name" value="{{request.validation.values['user_name']}}" autofocus/>
         <div id="user_name_error" class="error{% if not request.validation.errors['user_name'] %} hidden{% endif %}">
            {{request.validation.errors['user_name']}}
        </div>
    </div>




    <div class="grid-7" style="clear:left">
        <label>
            GitHub Repo Name
            <span class="tooltip" title="The name of any repository in your Github repository list.">{{  g.icons['help'].render() }}</span>
        </label>

    </div>
     <div class="grid-10">
        <input id="project_name" name="project_name" value="{{request.validation.values['project_name']}}" />
        <div id="project_name_error" class="error{% if not request.validation.errors['project_name'] %} hidden{% endif %}">
            {{request.validation.errors['project_name']}}
        </div>
    </div>

    <div class="grid-10 field-divider">
        <label>Then</label>
    </div>

    <div class="grid-16" style="clear: left;">
        <p>Pick a URL</p>
    </div>
    <div class="grid-7" style="clear:left">
        <label>{{config.site_name}} URL Name
        <span class="tooltip" title="A link-friendly name for your project's URL.">{{  g.icons['help'].render() }}</span>
        </label>
    </div>
    <div class="grid-10">
        <input id="project_shortname" name="project_shortname" value="{{request.validation.values['project_shortname']}}"/>
        <div id="project_shortname_error" class="error{% if not request.validation.errors['project_shortname'] %} hidden{% endif %}">
            {{request.validation.errors['project_shortname']}}
        </div>
        <div id="project-url">
            http://{{request.environ['HTTP_HOST']}}{{importer.neighborhood.url()}}<span id="url-fragment">{{request.validation.values['project_shortname']}}</span>
        </div>
    </div>
    <script>
    $(window).on('load', function() {

        function name(str) {
            return str ? str.replace(/^\W+|\.git$/g, '') : null;
        }

        function owner(str) {
            if (!str) return null;
            var idx = str.indexOf(':');
            if (idx > -1) {
                return str.slice(idx + 1);
            }
            return str;
        }

        $('#project_url').on('keyup', function (evt) {
            $('#project_url_error').toggleClass('hidden', true);
            if (!$(this).val()) {
                $('#project_name').val('');
                $('#user_name').val('');
                return;
            }
            try {
                var url = new window.URL($(this).val());
            }catch(e){
                $('#project_url_error').empty().append('<p>Not a valid URL</p>');
                $('#project_url_error').toggleClass('hidden', false);
                return
            }
            var pieces = url.pathname.split('/').filter(Boolean);
            var _owner = owner(pieces[0]);
            var _name = name(pieces[1]);
            $('#project_name').val(_name);
            $('#user_name').val(_owner);
            $('#project_name').trigger('change');

        })
    })
    </script>
    <style>
    .field-divider{clear: left;padding-top:5px;padding-bottom:5px;};
    </style>
{% endblock %}
