package com.unibuy.smartdevice.tools;

import com.unibuy.smartdevice.exception.LogsIOException;
import com.unibuy.smartdevice.exception.LogsMalformedURLException;

import java.util.Map;

public abstract class FormGetMainScheduler extends HandlerMainScheduler {
    private String urlString;
    private String cookie;
    private String referer;
    private Map<String, String> mapByRequestProperty;

    public FormGetMainScheduler(HandlerMain srcHandlerMain) {
        super(srcHandlerMain);
    }

    @Override
    protected void execute(HandlerMain handlerMain) {
        try {
            HttpConnect httpConnect = new HttpConnect();
            int responseCode = httpConnect.formGet(getUrlString(), getCookie(), getReferer(), mapByRequestProperty);
            getLogs().info("(" + responseCode + ")receive:" + httpConnect.getRecevieData());
            response(responseCode, httpConnect.getRecevieData(), handlerMain);
        } catch (LogsMalformedURLException | LogsIOException e) {
            getLogs().warning(e);
        }
    }

    public void start(String urlString, String cookie, String referer, Map<String, String> mapByRequestProperty) {
        this.urlString = urlString;
        this.cookie = cookie;
        this.referer = referer;
        this.mapByRequestProperty = mapByRequestProperty;
        super.start();
    }

    public abstract void response(int responseCode, String receive, HandlerMain handlerMain);

    String getUrlString() {
        return urlString;
    }

    String getCookie() {
        return cookie;
    }

    String getReferer() {
        return referer;
    }
}
