package com.lasarobotics.ftc.monkeyc;

import android.content.Context;

import com.google.gson.Gson;
import com.lasarobotics.ftc.controller.Controller;
import com.lasarobotics.ftc.util.Timers;
import com.qualcomm.robotcore.hardware.Gamepad;

import java.util.ArrayList;

/**
 * The MonkeyC (MonkeySee) library that handles recording and storing driver controls
 * These controls can be inserted during runtime (when the robot is moving)
 * or can be created prior to a match.  MonkeyDo can then execute these commands.
 */
public class MonkeyC {
    ArrayList<MonkeyData> commands1;
    ArrayList<MonkeyData> commands2;
    Controller previous1 = new Controller();
    Controller previous2 = new Controller();
    Timers t;

    public MonkeyC()
    {
        this.commands1 = new ArrayList<MonkeyData>();
        this.commands2 = new ArrayList<MonkeyData>();
        t = new Timers();
        t.startClock("global");
    }

    public void add(Controller c1, Controller c2)
    {
        //Make copy of controller
        Controller local1 = new Controller(c1);
        Controller local2 = new Controller(c2);

        //Get current time stamp
        long time = t.getClockValue("global");

        //Get previous controller state JSON
        Gson gson = new Gson();
        //GsonBuilder gsonbuild = new GsonBuilder();

        //String json1 = gson.toJson(local1);
        //JsonObject jsobj = new JsonObject();

        //Get controller deltas
        MonkeyData data = MonkeyUtil.createDeltas(local1, previous1, local2, previous2, time);

        //Update previous values to current values
        this.previous1 = local1;
        this.previous2 = local2;

        //Write to the instruction array for writing to disk later
        commands1.add(data);
    }

    public void add(Gamepad instruction, Gamepad instruction2)
    {
        //TODO test this - status on update() may remain either just pressed or just unpressed
        Controller one = new Controller(instruction);
        Controller two = new Controller(instruction2);
        add(one, two);
    }

    public void clear()
    {
        commands1.clear();
        commands2.clear();
    }

    public void write(String filename,Context context)
    {
        MonkeyUtil.writeFile(filename, commands1, context);
    }
}

@@ -577,46 +577,8 @@
 ands
-1;%0A    ArrayList%3CMonkeyData%3E commands2
 ;%0A  
@@ -732,63 +732,8 @@
 ands
-1 = new ArrayList%3CMonkeyData%3E();%0A        this.commands2
  = n
@@ -1647,17 +1647,16 @@
 commands
-1
 .add(dat
@@ -2019,36 +2019,8 @@
 ands
-1.clear();%0A        commands2
 .cle
@@ -2140,17 +2140,16 @@
 commands
-1
 , contex

Remove unnecessary command storage