package com.unibuy.smartdevice.tools;

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

import java.util.Map;

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

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

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

    public void start(String urlString, String postData, String cookie, String referer) {
        this.urlString = urlString;
        this.data = postData;
        this.cookie = cookie;
        this.referer = referer;
        super.start();
    }

    public void start(String urlString, String postData, String cookie, String referer, Map<String, String> mapByRequestProperty) {
        this.urlString = urlString;
        this.data = postData;
        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 getData() {
        return data;
    }

    String getCookie() {
        return cookie;
    }

    String getReferer() {
        return referer;
    }
}
