<?php
namespace WpAssetCleanUp\OptimiseAssets\ResourceLoading;

use WpAssetCleanUp\Misc;
use WpAssetCleanUp\OptimiseAssets\ResourceLoading;

/**
 *
 */
class Exclusions
{
    /**
     * @param $imgTag
     * @param $imageIndex
     * @param $lazyLoadSettings
     *
     * @return bool
     */
    public static function excludeMatchedImgTag($imgTag, $imageIndex, $lazyLoadSettings)
    {
        $excludeFirst = $lazyLoadSettings['exclude_first'];

        // Note: $imageIndex starts from 0
        // e.g. if "Exclude First" is set to 2 images, then anything below 2 (0 and 1) is exluded
        if ($excludeFirst > 0 && $imageIndex < $excludeFirst) {
            return true;
        }

        // Exclude via "CSS Classes"
        $skipViaCssClassesTextarea = trim($lazyLoadSettings['skip_via_css_classes']);

        if ( $skipViaCssClassesTextarea ) {
            $imgTagClassValue = trim(Misc::getValueFromTag($imgTag, 'class'));

            if ($imgTagClassValue &&
                Exclusions::excludeImgHasClassInSkippedClassesTextarea($imgTagClassValue, $skipViaCssClassesTextarea)) {
                return true;
            }
        }

        // Exclude via "URL Keywords"
        $skipViaSourceKeywordsTextarea = trim($lazyLoadSettings['skip_via_source_keywords']);

        if ($skipViaSourceKeywordsTextarea &&
            Exclusions::excludeImgHasSourceMatchesFromSkippedUrlKeywords($imgTag, $skipViaSourceKeywordsTextarea)) {
            return true;
        }

        return false;
    }

    /**
     * @param $imgTag
     * @param $skipViaSourceKeywordsTextarea
     *
     * @return bool
     */
    public static function excludeImgHasSourceMatchesFromSkippedUrlKeywords($imgTag, $skipViaSourceKeywordsTextarea)
    {
        $allPossbileSourcesKeywordsToExclude = array();

        if (strpos($skipViaSourceKeywordsTextarea, "\n") === false) {
            $allPossbileSourcesKeywordsToExclude[] = $skipViaSourceKeywordsTextarea;
        } else {
            foreach (explode("\n", $skipViaSourceKeywordsTextarea) as $possibleSourceLine) {
                if (trim($possibleSourceLine)) {
                    $allPossbileSourcesKeywordsToExclude[] = $possibleSourceLine;
                }
            }
        }

        $matchBy = 'source';

        $allSourceValuesFromImgTag = ResourceLoading::getImgTagValuesToMatchAgainst($imgTag, $matchBy);

        if (empty($allSourceValuesFromImgTag[$matchBy]) || ! is_array($allSourceValuesFromImgTag[$matchBy])) {
            return false;
        }

        foreach ($allPossbileSourcesKeywordsToExclude as $possibleSourceKeywordToExclude) {
            // On each line from the textarea (e.g. if there are multiple lines)
            // Go through all the possible sources found: 'src', 'srcset', 'data-src', 'data-srcset'
            foreach ($allSourceValuesFromImgTag[$matchBy] as $sourceValueFromImgTag) {
                if (ResourceLoading::isMatchViaKeyword($sourceValueFromImgTag, $possibleSourceKeywordToExclude)) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * @param $imgTagClassValue
     * @param $skipViaCssClassesTextarea
     *
     * @return bool
     */
    public static function excludeImgHasClassInSkippedClassesTextarea($imgTagClassValue, $skipViaCssClassesTextarea)
    {
        $allSkipCssClassesPerLine = array();

        $skipViaCssClassesTextarea = trim($skipViaCssClassesTextarea);

        if (strpos($skipViaCssClassesTextarea, "\n") === false) {
            $allSkipCssClassesPerLine[] = $skipViaCssClassesTextarea;
        } else {
            foreach (explode("\n", $skipViaCssClassesTextarea) as $skipViaCssLine) {
                $allSkipCssClassesPerLine[] = trim($skipViaCssLine);
            }
        }

        foreach ($allSkipCssClassesPerLine as $skipThisCssClass) {
            if (ResourceLoading::imgClassAttrIsMatchedFromClassSelector($imgTagClassValue, $skipThisCssClass)) {
                return true; // exclude image from lazy load
            }
        }

        return false;
    }

}