import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import util.Log;

import map.data.City;
import map.store.ChainStore;

/**
 * サークルＫの店舗情報を取得します。
 * @author Masayasu Fujiwara
 */
public class CircleKPlugin extends ChainStore {

	/**
	 * 住所切り出しパターン
	 */
	private final Pattern ADDRESS_PARSER;

	public CircleKPlugin() {
		super("サークルK");
		this.ADDRESS_PARSER = Pattern.compile("<td width=\"296\" bgcolor=\"#[0-9A-F]{6}\" class=\"text12\">([^<>]+)</td>");
	}

	@Override
	protected URL getURL(City city, int page) {
		URL url = null;
		try {
			url = new URL("http://sp.chizumaru.com/dbh/200080/list.aspx?account=200080&accmd=0&arg=&c1=1&c2=&c3=&c4=&c5=&c6=&c7=&c8=&c9=&c10=&c11=&c12=&c13=&c14=&c15=&c16=&c17=&c18=&c19=&c20=&c21=&c22=&c23=&c24=&c25=&c26=&c27=&c28=&c29=&c30=&pg="+ page +"&adr=" + City.cityFormat(city.getCode()));
		} catch (MalformedURLException e) {
			Log.err(this, e);
		}
		return url;
	}

	@Override
	protected String parseAddress(String input) {
		Matcher matcher = this.ADDRESS_PARSER.matcher(input);
		if (matcher.find()) {
			return matcher.group(1);
		}
		return null;
	}
}
