{% extends 'base.html' %}
{% block title %}IvoryOS | New devices{% endblock %}

{% block body %}
    <div class="row">
        <div class="col-xl-4 col-lg-4 col-md-6 mb-4 ">
            <h5>Available Python API</h5>
            <hr>
            {% for instrument in api_variables %}
                <div class="bg-white rounded shadow-sm position-relative">
                    <h5 class="p-3 controller-card">
                        <a href="{{ url_for('control.new_controller', instrument=instrument) }}" class="text-dark stretched-link">{{instrument}}</a>
                    </h5>
                </div>
            {% endfor %}
            <div class="bg-white rounded shadow-sm position-relative">
                <h5 class="p-3 controller-card">
                    <a data-bs-toggle="modal" href="#importAPI" class="stretched-link"><i class="bi bi-folder-plus"></i> Import API</a>
                </h5>
            </div>
        </div>
        <div class="col-xl-5 col-lg-5 col-md-6 mb-4 ">
            {% if device %}
                <h5>Connecting</h5><hr>
                <form role="form" method='POST' name="init" action="{{ url_for('control.new_controller', instrument=instrument) }}">
                    <div class="form-group">
                        <div class="input-group mb-3">
                            <span class="input-group-text" >Name this device</span>
                            <input class="form-control" type="text" id="device_name" name="device_name" aria-labelledby="nameHelpBlock" placeholder="e.g. {{device.__name__}}_1" >
                            <div id="nameHelpBlock" class="form-text">
                                Name your instrument, avoid names that are defined on the right
                            </div>
                        </div>
                        {% for arg in device.__init__.__annotations__ %}
                            {% if not arg == "return" %}
                                <div class="input-group mb-3">
                                    <span class="input-group-text" >{{arg}}</span>
                                    <input class="form-control"  type="text" id="{{arg}}" name="{{arg}}"
                                           placeholder="{{device.__init__.__annotations__[arg].__name__}}"
                                           value="{{args.parameters[arg].default if not args.parameters[arg].default.__name__ == '_empty' else ''}}">
                                    {% if device.__init__.__annotations__[arg].__module__ is not in ["builtins", "typing"] %}
                                        <a role="button" href="{{ url_for('control.new_controller', instrument=device.__init__.__annotations__[arg].__name__) }}" class="btn btn-secondary">initialize {{device.__init__.__annotations__[arg].__name__}} first</a>
                                    {% endif %}
                                </div>
                            {% endif %}
                        {% endfor %}
                        <button type="submit" class="btn btn-dark">Connect</button>
                    </div>
                </form>
            {% endif %}
        </div>
        <div class="col-xl-3 col-lg-3 col-md-6 mb-4">
            <h5>Defined Instruments</h5><hr>
            {% if defined_variables %}
                <ul class="list-group">
                    {% for instrument in defined_variables %}
                        <li class="list-group-item">{{instrument}}</li>
                    {% endfor %}
                </ul>
            {% endif %}
        </div>
    </div>


    <div class="modal fade" id="importAPI" tabindex="-1" aria-labelledby="importModal" aria-hidden="true" >
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h1 class="modal-title fs-5" id="importModal">Import API by file path</h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form method="POST" action="{{ url_for('control.import_api') }}" enctype="multipart/form-data">
                    <div class="modal-body">
                        <h5>input manually</h5>
                        <div class="input-group mb-3">
                            <label class="input-group-text" for="filepath">File Path:</label>
                            <input type="text" class="form-control" name="filepath" id="filepath">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"> Close </button>
                            <button type="submit"  class="btn btn-primary"> Save </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

{% endblock %}
