import * as bootstrap from 'bootstrap';

export default class ProfileConfigurator extends HTMLElement
{
    constructor()
    {
        super();

        this._shadowRoot = this.attachShadow({ mode: 'closed' });
    
        this._shadowRoot.innerHTML = `
            <link href="./assets/css/bootstrap.min.css" rel="stylesheet" />
            <link href="./assets/icons/css/fontawesome.min.css" rel="stylesheet" />
            <link href="./assets/icons/css/solid.min.css" rel="stylesheet" />

            <button id="profileButton" type="button" class="btn btn-outline-primary btn-sm"><i class="fa-solid fa-user"></i></button>
            <strong class="profileNameLabel text-secondary"></strong>

            <div class="modal" id="profileModal" tabindex="-1" aria-labelledby="profileModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h1 class="modal-title fs-5" id="profileModalLabel">User Profile</h1>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body p-0">
                            <div class="p-3 bg-body-tertiary rounded-3">
                                <strong>Name:</strong> <span class="profileNameLabel"></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        `;

        this.userNameLabels = this._shadowRoot.querySelectorAll('.profileNameLabel');
        this.profileButton = this._shadowRoot.querySelector('#profileButton');

        // Create profile modal
        this.profileModal = new bootstrap.Modal(this._shadowRoot.querySelector('#profileModal'), {
            backdrop: true,
            focus: true,
            keyboard: true
        });

        // Bind event listeners
        this.boundOnProfileButtonClick = this.onProfileButtonClick.bind(this);
    }

    connectedCallback()
    {
        // Add profile button click listener
        this.profileButton.addEventListener('click', this.boundOnProfileButtonClick);
    }

    displayUserName(name)
    {
        for (let userNameLabel of this.userNameLabels)
            userNameLabel.innerText = name;
    }

    onProfileButtonClick(e)
	{
		this.profileModal.show();
	}
}

customElements.define('profile-configurator', ProfileConfigurator);