//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package kinosearch.core.browser;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class SimpleBrowser implements Browser {
    private Map<String, String> headers = new HashMap();
    private String encoding = "UTF-8";

    public Browser setHeader(String name, String value) {
        this.headers.put(name, value);
        return this;
    }

    public String get(String url) {
        String result = "";

        try {
            HttpURLConnection connection = this.create_connection(url);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() != 200) {
                return result;
            }

            result = this.readOutput(connection.getInputStream());
            connection.disconnect();
        } catch (IOException var4) {
        }

        return result;
    }

    public String post(String url, String data) {
        String result = "";

        try {
            HttpURLConnection connection = this.create_connection(url);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
            connection.setDoOutput(true);
            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
            dos.writeBytes(data);
            dos.flush();
            dos.close();
            if (connection.getResponseCode() != 200) {
                return "";
            }

            result = this.readOutput(connection.getInputStream());
            connection.disconnect();
        } catch (IOException var6) {
        }

        return result;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    private HttpURLConnection create_connection(String strUrl) throws IOException {
        URL url = new URL(strUrl);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setUseCaches(false);
        connection.setRequestProperty("Connection", "close");
        connection.setRequestProperty("Accept-Encoding", "deflate");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 4.2.2; GT-I9505 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36");

        for(Map.Entry<String, String> entry : this.headers.entrySet()) {
            connection.setRequestProperty((String)entry.getKey(), (String)entry.getValue());
        }

        return connection;
    }

    private String readOutput(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(in, this.encoding));

        String str;
        while((str = br.readLine()) != null) {
            sb.append(str).append('\n');
        }

        br.close();
        return sb.toString();
    }
}
