const Attribute = require(ATTRIBUTE_CLASS_PATH);

/**
 * Usage example
 * 
 */
class List extends Attribute {
    /**
     * {@inheritdoc}
     */
    map() {
        return {
            'icon': this.staticIcon,
            '[icon]': this.dynamicIcon,
            'spin': this.spinIcon,
            '[spin]': this.dynamicSpinIcon,
            'fab': this.brandIcon,
            '[fab]': this.dynamicBrandIcon,
            'far': this.regularIcons,
            '[far]': this.dynamicRegularIcons,
        };
    }

    /**
     * {@inheritdoc}
     */
    staticIcon(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = this.forcePull(attribute).split(' ');

        let icon = this.iconWithOtherClasses.shift();

        this.setIcon(`'${icon}'`);
    }

    isFontAwesomeTag() {
        return this.tag.originalElement.tagName.toLowerCase() == 'fa';
    }

    /**
     * {@inheritdoc}
     */
    dynamicIcon(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = [];

        this.setIcon(this.forcePull(attribute));
    }

    /**
     * {@inheritdoc}
     */
    brandIcon(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = this.forcePull(attribute).split(' ');

        let icon = this.iconWithOtherClasses.shift();

        this.set('fab', true);

        this.setIcon(`'${icon}'`);
    }

    /**
     * {@inheritdoc}
     */
    dynamicBrandIcon(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = [];

        this.set('fab', true);

        this.setIcon(this.forcePull(attribute));
    }

    /**
     * {@inheritdoc}
     */
    regularIcons(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = this.forcePull(attribute).split(' ');

        let icon = this.iconWithOtherClasses.shift();

        this.set('far', true);

        this.setIcon(`'${icon}'`);
    }

    /**
     * {@inheritdoc}
     */
    dynamicRegularIcons(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = [];

        this.set('far', true);

        this.setIcon(this.forcePull(attribute));
    }

    /**
     * {@inheritdoc}
     */
    spinIcon(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = this.forcePull(attribute).split(' ');

        let icon = this.iconWithOtherClasses.shift();

        this.set('spin', true);

        this.setIcon(`'${icon}'`);
    }

    /**
     * {@inheritdoc}
     */
    dynamicSpinIcon(attribute) {
        if (! this.isFontAwesomeTag()) return;
        this.iconWithOtherClasses = [];

        this.set('spin', true);

        this.setIcon(this.forcePull(attribute));
    }

    setIcon(icon) {        
        let additionalClasses = this.iconWithOtherClasses;

        if (this.has('spin')) {
            this.forcePull('spin');
            additionalClasses.unshift('fa-spin');
        }

        additionalClasses.push('icon');

        if (this.has('class')) {
            additionalClasses.push(this.forcePull('class'));
        }

        // check the sizes
        for (let size = 1; size <= 10; size++) {
            if (this.has(`${size}x`)) {
                this.forcePull(`${size}x`);
                additionalClasses.push(`fa-${size}x`);
            }            
        }

        for (let size of ['xs', 'sm', 'lg']) {
            if (this.has(size)) {
                this.forcePull(size);
                additionalClasses.push('fa-' + size);
            }
        }

        let classes = additionalClasses.join(' ');

        let functionName = 'fas';

        if (this.has('fab')) {
            functionName = 'fab';
            this.forcePull('fab');
        } else if (this.has('far')) {
            functionName = 'far';
            this.forcePull('far');
        }

        this.tag.attributes.class = this.tag.var(`${functionName}(${icon}) + ' ${classes}'`);
    }
}

module.exports = List;