/**
 * MediaSniper 3.0 (2008-08-02)
 * Copyright 2007 - 2008 Zach Scrivena
 * zachscrivena@gmail.com
 * http://mediasniper.sourceforge.net/
 *
 * Simple program for downloading media files from popular websites.
 *
 * TERMS AND CONDITIONS:
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.freeshell.zs.mediasniper;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


/**
 * Represent a definition for parsing a webpage.
 */
class Definition
{
    /** name for this definition */
    private final String name;

    /** key-value map for this definition */
    private final Map<String,String> map = new HashMap<String,String>();

    /** key-value map of regex patterns for this definition */
    private final Map<String,Pattern> patternMap = new HashMap<String,Pattern>();


    /**
    * Constructor.
    *
    * @param name
    *     name for this definition
    */
    Definition(
            final String name)
    {
        this.name = name;
    }


    /**
    * Get the name for this definition.
    *
    * @return
    *     name for this definition
    */
    String getName()
    {
        return name;
    }


    /**
    * Get the value mapped to the specified key in this definition.
    *
    * @param key
    *     key whose associated value is to be returned
    * @return
    *     value mapped to the specified key
    * @throws java.lang.NullPointerException
    *     if the key does not exist in the map
    */
    String get(
            final String key)
            throws NullPointerException
    {
        final String value = map.get(key);

        if (value == null)
        {
            throw new NullPointerException("key does not exist in the map");
        }

        return value;
    }


    /**
    * Put the specified key-value mapping into the definition.
    *
    * @param key
    *     key for the mapping
    * @param value
    *     value for the mapping
    * @throws java.lang.NullPointerException
    *     if either the key or value is null
    */
    void put(
            final String key,
            final String value)
            throws NullPointerException
    {
        if (key == null)
        {
            throw new NullPointerException("key must not be null");
        }

        if (value == null)
        {
            throw new NullPointerException("value must not be null");
        }

        map.put(key, value);

        /* compile regex pattern, if necessary */
        if (key.endsWith(".match"))
        {
            try
            {
                patternMap.put(key, Pattern.compile(value));
            }
            catch (PatternSyntaxException e)
            {
                /* ignore */
            }
        }
    }


    /**
    * Get the regex pattern for the specified key.
    *
    * @param key
    *     key whose associated regex pattern is to be returned
    * @return
    *     regex pattern for the specified key
    * @throws java.lang.NullPointerException
    *     if the key does not exist in the pattern map
    */
    Pattern getPattern(
            final String key)
            throws NullPointerException
    {
        final Pattern pattern = patternMap.get(key);

        if (pattern == null)
        {
            throw new NullPointerException("key does not exist in the pattern map");
        }

        return pattern;
    }


    /**
    * Evaluate the string value for the specified target key, by substituting the
    * capturing groups for the matched regex pattern strings.
    *
    * @param targetKey
    *     target key whose string value is to be evaluated
    * @param matchedPatterns
    *      key-CapturingGroups map for matched regex pattern strings
    * @return
    *     evaluated target string with the appropriate matched values substituted
    * @throws java.lang.NullPointerException
    *     if any specified key is null, or does not exist in the map
    */
    String evaluateTargetString(
            final String targetKey,
            final Map<String,String[]> matchedPatterns)
            throws NullPointerException
    {
        if (targetKey == null)
        {
            throw new NullPointerException("targetKey must not be null");
        }

        String targetValue = map.get(targetKey);

        if (targetValue == null)
        {
            throw new NullPointerException("targetKey does not exist in the map");
        }

        for (Map.Entry<String,String[]> e : matchedPatterns.entrySet())
        {
            final String s = e.getKey();
            final String[] groups = e.getValue();

            for (int k = 0; k < groups.length; k++)
            {
                /* replace all occurences of "<s.k>" in the targetValue with the */
                /* corresponding matched capturing group                               */
                targetValue = targetValue.replaceAll(Pattern.quote("<" + s + "." + k + ">"), groups[k]);
            }
        }

        return targetValue;
    }


    /**
    * Return the capturing groups for the specified Matcher object, as an array of Strings.
    *
    * @param m
    *     Matcher object containing the capturing groups
    * @return
    *     array of Strings containing the capturing groups
    */
    static String[] capturingGroupsAsArray(
            final Matcher m)
    {
        final String[] groups = new String[m.groupCount() + 1];

        for (int i = 0; i < groups.length; i++)
        {
            groups[i] = m.group(i);
        }

        return groups;
    }
}