import { Grid, createElement, Popup } from "../../ui";
import { nullOrEmpty, r, contains } from "../../utility";

class Follower {
    #option;
    #grid;

    constructor(option = {}) {
        this.#option = option;
    }

    async show(title, parent = document.body) {
        const tabIndex = Math.max.apply(null, [...document.querySelectorAll('[tabindex]')].map(e => e.tabIndex ?? 0)) + 3;

        const gridContainer = createElement('div', 'follower-grid');
        const popup = new Popup({
            onMasking: this.#option.onMasking,
            title,
            content: createElement('div', 'follower-wrapper',
                createElement('div', div => div.innerText = r('whoWantReceiveCustomerNotification', 'Who do you want to receive customer notifications?')),
                createElement('input', search => {
                    search.type = 'text';
                    search.tabIndex = tabIndex + 3;
                    search.className = 'ui-input follower-search';
                    search.addEventListener('input', () => {
                        const key = search.value;
                        if (nullOrEmpty(key)) {
                            this.#grid.source = this.#option.followers.sort(function (a, b) { return ((b.Text || b.Email) ? 1 : 0) - ((a.Text || a.Email) ? 1 : 0) });
                        } else {
                            this.#grid.source = this.#option.followers.filter(f => f.Text || f.Email || contains(f.DisplayName, key, true))
                                .sort(function (a, b) { return ((b.Text || b.Email) ? 1 : 0) - ((a.Text || a.Email) ? 1 : 0) });
                        }
                    });
                }),
                gridContainer
            ),
            buttons: [
                {
                    text: r('ok', 'OK'),
                    key: 'ok',
                    trigger: () => {
                        if (typeof this.#option.onOk === 'function') {
                            return this.#option.onOk.call(this, this.#grid.source.filter(f => f.Email || f.Text));
                        }
                    }
                },
                { text: r('cancel', 'Cancel'), key: 'cancel' }
            ]
        });
        const result = await popup.show(parent);
        result.querySelector('.follower-search').focus();
        // grid
        const grid = new Grid(gridContainer);
        grid.columns = [
            { key: 'DisplayName', caption: r('contactName', 'Contact Name'), width: 240 },
            { key: 'ContactTypeName', caption: r('contactType', 'Contact Type'), width: 120 },
            {
                key: 'Text',
                caption: r('text', 'Text'),
                type: Grid.ColumnTypes.Checkbox,
                width: 60,
                enabled: item => !nullOrEmpty(item.Mobile)
            },
            {
                key: 'Email',
                caption: r('email', 'Email'),
                type: Grid.ColumnTypes.Checkbox,
                width: 70,
                // enabled: item => !nullOrEmpty(item.ID)
            }
        ];
        grid.init();
        grid.source = this.#option.followers.sort(function (a, b) { return ((b.Text || b.Email) ? 1 : 0) - ((a.Text || a.Email) ? 1 : 0) });
        this.#grid = grid;
        return result;
    }
}

export default Follower;