/**
 * Multimud Helper.
 * 
 * Copyright (C) 2009 Benjamin Lerman<br>
 * Copyright (C) 2009 Jacques ???
 * 
 * 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 net.ambre.multimud_helper.parser.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.ambre.multimud_helper.bean.SkillLevel;
import net.ambre.multimud_helper.bean.TrainedSkill;
import net.ambre.multimud_helper.dao.TrainerDAO;
import net.ambre.multimud_helper.parser.TrainedSkillParser;

/**
 * implementation of {@link TrainedSkillParser}.
 */
public class TrainedSkillParserImpl implements TrainedSkillParser {

    /**
     * the {@link TrainerDAO} to use.
     */
    private TrainerDAO trainerDAO;

    /**
     * the regular expression that recognize a line of a skill.
     */
    private static final String SKILL_REGEXP = "^ *([a-z ]*) from  *\\(([a-z ]*)\\) to  *\\(([a-z ]*)\\)";

    /**
     * the {@link Pattern} to parse a line of skill.
     */
    private static final Pattern SKILL_PATTERN = Pattern.compile(SKILL_REGEXP);

    /**
     * {@inheritedDoc}
     */
    public TrainedSkill parseTrainedSkill(String trainedSkill) {
        Matcher matcher = SKILL_PATTERN.matcher(trainedSkill);
        if (!matcher.matches()) {
            throw new IllegalArgumentException(trainedSkill + " does not correspond to the regular expression: " + SKILL_REGEXP);
        }
        TrainedSkill trainedSkillObject = new TrainedSkill();
        trainedSkillObject.setSkill(getTrainerDAO().getSkill(matcher.group(1)));
        trainedSkillObject.setMinLevel(SkillLevel.parseSkillLevel(matcher.group(2)));
        trainedSkillObject.setMaxLevel(SkillLevel.parseSkillLevel(matcher.group(3)));
        return trainedSkillObject;
    }

    /**
     * {@inheritedDoc}
     */
    public SortedSet<TrainedSkill> parseTrainedSkills(String trainedSkills) {
        try {
            SortedSet<TrainedSkill> result = new TreeSet<TrainedSkill>();
            BufferedReader br = new BufferedReader(new StringReader(trainedSkills));
            String currentLine = br.readLine();
            while (currentLine != null) {
                result.add(parseTrainedSkill(currentLine));
                currentLine = br.readLine();
            }
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * getter for the trainerDAO property.
     * 
     * @return the trainerDAO.
     */
    public TrainerDAO getTrainerDAO() {
        return this.trainerDAO;
    }

    /**
     * setter for the trainerDAO property.
     * 
     * @param trainerDAO the trainerDAO to set.
     */
    public void setTrainerDAO(TrainerDAO trainerDAO) {
        this.trainerDAO = trainerDAO;
    }

}
