package com.emapco.subaChan.util;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.*;
import java.net.URL;

public class Download {


    public static void downloadThumbnail(URL url, String tbName, String boardName) throws IOException{

        String imgExt = getImgExt(url.toString());
        String destinationFile = tbName + imgExt;

        if (System.getProperty("os.name").contains("Linux")) {
            LinuxSaveDirctories.makeThumbnailDir(boardName);

            try {

                InputStream in = new BufferedInputStream(url.openStream());
                OutputStream out = new FileOutputStream(LinuxSaveDirctories.getBoardThumbnailFolder(boardName) + destinationFile);
                byte[] buf = new byte[2048];
                int n;
                while (-1!=(n=in.read(buf))) {
                    out.write(buf, 0, n);
                }
                out.close();
                in.close();

            } catch(IOException e) {
                System.out.println("Could not download thumbnail");
            }
        } else if (System.getProperty("os.name").contains("Windows")) {
              WindowsSaveDirctories.makeThumbnailDir(boardName);

              try {

                  InputStream in = new BufferedInputStream(url.openStream());
                  OutputStream out = new FileOutputStream(WindowsSaveDirctories.getBoardThumbnailFolder(boardName) + destinationFile);
                  byte[] buf = new byte[2048];
                  int n;
                  while (-1!=(n=in.read(buf))) {
                      out.write(buf, 0, n);
                  }
                  out.close();
                  in.close();

              } catch(IOException e) {
                  System.out.println("Could not download thumbnail");
              }

        }
    }


    public static void downloadImg(URL url, String imageExt, String imageName, String  boardName) throws IOException {

        String destinationFile = imageName + imageExt;

        if (System.getProperty("os.name").contains("Linux")) {
            LinuxSaveDirctories.makeImageDir(boardName);

            try {

                InputStream in = new BufferedInputStream(url.openStream());
                OutputStream out = new FileOutputStream(LinuxSaveDirctories.getBoardImageFolder(boardName) + destinationFile);
                byte[] buf = new byte[2048];
                int n;
                while (-1!=(n=in.read(buf))) {
                    out.write(buf, 0, n);
                }
                out.close();
                in.close();

            } catch(IOException e) {
                System.out.println("Could not download image");
            }
        } else if (System.getProperty("os.name").contains("Windows")) {
            WindowsSaveDirctories.makeImageDir(boardName);

            try {

                InputStream in = new BufferedInputStream(url.openStream());
                OutputStream out = new FileOutputStream(WindowsSaveDirctories.getBoardImageFolder(boardName) + destinationFile);
                byte[] buf = new byte[2048];
                int n;
                while (-1!=(n=in.read(buf))) {
                    out.write(buf, 0, n);
                }
                out.close();
                in.close();

            } catch(IOException e) {
                System.out.println("Could not download image");
            }

        }
    }


    private static String getImgExt(String stringUrl) {
        return "." + stringUrl.substring(stringUrl.length() - 3);
    }


    public static String downloadJsonString(final URL url) {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url.toString());
        ResponseHandler<String> response = new BasicResponseHandler();
        String responseBody = null;
        try {
            responseBody = client.execute(get, response);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.getConnectionManager().shutdown();
        }
        return responseBody;
    }

}
