package yuka.detectors;

import yuka.containers.News;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by imyuka on 18/08/2016.
 */
public class FigureDetector {
    private Map<String, Integer> cmap;
    private Map<String, Integer> pmap;
    private Map<String, Integer> dim;
    //private int height = 0;
    //private int height = 0;

    public FigureDetector(Map<String, String> figure) {
        String url = figure.get(News.IMAGE_URL);
        dim = getDimension(url);
        String caption = figure.get(News.IMAGE_CAPTION);
        cmap = ClubDetector.count(caption);
        PlayerDetector pd = new PlayerDetector(cmap.keySet());
        pmap = pd.count(caption);
    }

    public int getHeight() {
        return dim.get("height");
    }

    public int getWidth() {
        return dim.get("width");
    }

    public double getPercentage (double totalHeight) {
        return (double) getWidth() / HeightDetector.COLUMN_WIDTH
                * (getHeight() / totalHeight);
    }

    public Map<String, Integer> getClubMap() {
        return cmap;
    }

    public Map<String, Integer> getPlayerMap() {
        return pmap;
    }

    public static Map<String, Integer> getDimension (String source) {
        //int[] dim = new int[]{0,0};
        Map<String, Integer> dim = new HashMap<>();
        dim.put("width", 0);
        dim.put("height", 0);
        try {
            URL url = new URL(source);
            URLConnection conn = url.openConnection();

            // now you get the content length
            /////int contentLength = conn.getContentLength();
            // you can check size here using contentLength

            InputStream in = conn.getInputStream();
            BufferedImage image = ImageIO.read(in);

            // you can get size  dimesion
            //int width = image.getWidth();
            //int height = image.getHeight();
            dim.put("width", image.getWidth());
            dim.put("height", image.getHeight());
        } catch (IOException e) {
            System.out.println();
        }
        return dim;
    }
}
