package com.github.f1xman.movifier2.domain.movie;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.String.format;

public class ImdbId {

    private static final String RAW_IMDB_ID_PATTERN = "tt\\d+";
    private static final Pattern imdbIdPattern = Pattern.compile(format("(%s)", RAW_IMDB_ID_PATTERN));

    private String payload;

    private ImdbId(String imdbId) {
        this.payload = imdbId;
    }

    public static ImdbId parse(String id) {
        Matcher matcher = imdbIdPattern.matcher(id);
        if (matcher.find()) {
            String imdbId = matcher.group(1);
            return new ImdbId(imdbId);
        } else {
            throw new IllegalArgumentException(format("Given string (%s) doesn't contain imdb id", id));
        }
    }

    @Override
    public String toString() {
        return payload;
    }

    public String createUrl() {
        return "http://www.imdb.com/title/" + payload;
    }
}
