package com.noa.realtime;

import android.graphics.Canvas;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by Noa on 31/10/2017.
 */

public class Manager {

    private MyView view;
    private final Api api;
    private HashMap<String,Circle> circles = new HashMap<>();

    public Manager(MyView view) {

        this.view = view;
        this.api = new Api(this);
    }

    public void addCircle(float x, float y, int[] color) {

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();

        HashMap<String, Object> params = new HashMap<>();
        params.put("x", (x * 100) / this.view.getWidth());
        params.put("y", (y * 100) / this.view.getHeight());
        params.put("color", color);
        String json = gson.toJson(params);

        this.api.fetch("POST", "/api/add", json);
    }

    public void removeCircle(String uuid) {

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();

        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", uuid);

        String json = gson.toJson(params);

        this.api.fetch("POST", "/api/delete", json);
    }

    public void addCircleToDrawableList(String data) {

        try {
            Circle circle;
            JSONObject obj = new JSONObject(data);
            double x = (((double) obj.get("x")) * this.view.getWidth()) / 100;
            double y = (((double) obj.get("y")) * this.view.getHeight()) / 100;
            int r = Math.min(this.view.getWidth(), this.view.getHeight()) / 20;
            JSONArray color = (JSONArray) obj.get("color");
            String uuid = (String) obj.get("uuid");

            circle = new Circle((int) x, (int) y, r, new int[]{(int) color.get(0), (int) color.get(1), (int) color.get(2)});
            this.circles.put(uuid, circle);

//            this.view.setTest(true);
            this.view.postInvalidate();


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void removeCircleFromDrawableList(String data) {

        try {
            JSONObject obj = new JSONObject(data);
            String uuid = (String) obj.get("uuid");

            this.circles.remove(uuid);
            this.view.postInvalidate();


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void init() {

        String response = this.api.fetch("POST", "/api/init", "");
        try {
            JSONObject obj = new JSONObject(response);
            JSONObject data = (JSONObject) obj.get("data");
            int r = Math.min(this.view.getWidth(), this.view.getHeight()) / 20;
            Circle circle;

            for (Iterator iterator = data.keys(); iterator.hasNext(); ) {

                String key = (String) iterator.next();
                JSONObject val = (JSONObject) data.get(key);
                double x = (((double) val.get("x")) * this.view.getWidth()) / 100;
                double y = (((double) val.get("y")) * this.view.getHeight()) / 100;
                JSONArray color = (JSONArray) val.get("color");
                String uuid = (String) val.get("uuid");

                circle = new Circle((int) x, (int) y, r, new int[]{(int) color.get(0), (int) color.get(1), (int) color.get(2)});
                this.circles.put(uuid, circle);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void onDraw(Canvas canvas) {

        Circle circle;

        for(String key : this.circles.keySet()) {
            circle = this.circles.get(key);
            circle.onDraw(canvas);
        }
    }

    public View getView() {
        return view;
    }

    public HashMap<String, Circle> getCircles() {
        return this.circles;
    }
}
