package com.unibuy.smartdevice.tools;

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

import java.util.Map;

public abstract class FormPutMainScheduler extends HandlerMainScheduler {
    private String urlString;
    private String data;
    private String cookie;
    private String referer;
    private HttpConnect.ContentType contextType;
    private Map<String, String> mapByRequestProperty;

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


    @Override
    protected void execute(HandlerMain handlerMain) {
        try {
//            HttpConnect httpConnect = new HttpConnect(handlerMain.getContext());
//            int responseCode = httpConnect.formPut(getUrlString(), getData(), getCookie(), getReferer(), contextType);
            HttpConnect httpConnect = new HttpConnect();
            int responseCode = httpConnect.formPut(getUrlString(), getData(), getCookie(), getReferer(), contextType, 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, HttpConnect.ContentType contextType) {
        this.urlString = urlString;
        this.data = postData;
        this.cookie = cookie;
        this.referer = referer;
        this.contextType = contextType;
        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;
    }
}
