// Written by retoor@molodetz.nl

// This code defines custom web components to create and interact with a tile grid system for displaying images, along with an upload button to facilitate image additions.

// No external libraries or dependencies are used other than standard web components.


// MIT License
//
// 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.


class TileGridElement extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({ mode: 'open' });
        this.gridId = this.getAttribute('grid');
        this.component = document.createElement('div');
        this.shadowRoot.appendChild(this.component);
    }

    connectedCallback() {
        console.log('connected');
        this.styleElement = document.createElement('style');
        this.styleElement.textContent = `
            .grid {
                padding: 10px;
                display: flex;
                flex-wrap: wrap;
                gap: 10px;
                justify-content: center;
            }
            .grid .tile {
                margin: 10px;
                width: 100px;
                height: 100px;
                background-color: #ccc;
                object-fit: cover;
                border-radius: 10px;
                transition: transform 0.3s ease-in-out;
            }
            .grid .tile:hover {
                transform: scale(1.1);
            }
        `;
        this.component.appendChild(this.styleElement);
        this.container = document.createElement('div');
        this.container.classList.add('gallery');
        this.component.appendChild(this.container);
    }

    addImage(src) {
        const item = document.createElement('img');
        item.src = src;
        item.classList.add('tile');
        item.style.width = '100px';
        item.style.height = '100px';
        this.container.appendChild(item);
    }

    addImages(srcs) {
        srcs.forEach(src => this.addImage(src));
    }

    addElement(element) {
        element.classList.add('tile');
        this.container.appendChild(element);
    }
}

class UploadButton extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({ mode: 'open' });
        this.component = document.createElement('div');
        this.shadowRoot.appendChild(this.component);
        window.u = this;
    }

    get gridSelector() {
        return this.getAttribute('grid');
    }
    grid = null;

    addImages(urls) {
        this.grid.addImages(urls);
    }

    connectedCallback() {
        console.log('connected');
        this.styleElement = document.createElement('style');
        this.styleElement.textContent = `
        .upload-button {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        .upload-button input[type="file"] {
            display: none;
        }
        .upload-button label {
            display: block;
            padding: 10px 20px;
            background-color: #ccc;
            border-radius: 5px;
            cursor: pointer;
        }
        .upload-button label:hover {
            background-color: #999;
        }
        `;
        this.shadowRoot.appendChild(this.styleElement);
        this.container = document.createElement('div');
        this.container.classList.add('upload-button');
        this.shadowRoot.appendChild(this.container);
        const input = document.createElement('input');
        input.type = 'file';
        input.accept = 'image/*';
        input.multiple = true;
        input.addEventListener('change', (e) => {
            const files = e.target.files;
            const urls = [];
            for (let i = 0; i < files.length; i++) {
                const reader = new FileReader();
                reader.onload = (e) => {
                    urls.push(e.target.result);
                    if (urls.length === files.length) {
                        this.addImages(urls);
                    }
                };
                reader.readAsDataURL(files[i]);
            }
         });
        const label = document.createElement('label');
        label.textContent = 'Upload Images';
        label.appendChild(input);
        this.container.appendChild(label);
    }
}

customElements.define('upload-button', UploadButton);
customElements.define('tile-grid', TileGridElement);

class MeniaUploadElement extends HTMLElement {
    constructor(){
        super();
        this.attachShadow({ mode: 'open' });
        this.component = document.createElement("div");
        alert('aaaa');
        this.shadowRoot.appendChild(this.component);
    }

    connectedCallback() {
        this.container = document.createElement("div");
        this.component.style.height = '100%';
        this.component.style.backgroundColor = 'blue';
        this.shadowRoot.appendChild(this.container);

        this.tileElement = document.createElement("tile-grid");
        this.tileElement.style.backgroundColor = 'red';
        this.tileElement.style.height = '100%';
        this.component.appendChild(this.tileElement);

        this.uploadButton = document.createElement('upload-button');
        this.component.appendChild(this.uploadButton);
    }
}

customElements.define('menia-upload', MeniaUploadElement);