package main.Waypoints.PlanesCommands;

import main.models.Coordinate;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class SA342M {
    public static JSONArray getCommands(ArrayList<Coordinate> coords){
        /*
            NADIR, device 23
            Enter 		3004
			Del/Eff		3023
			Down arrow	3008
			selector cw	3003 value 1
			selector ccw 3003 value 0

            0 - 		3009
            1 - 		3010
            2 (north) - 3011
            3 - 		3012
            4 - 		3013
            5 - 		3014
            6 (east) - 	3015
            7 - 		3016
            8 - 		3017
            9 - 		3018
            
         */

        JSONArray commandArray = new JSONArray();


        for (Coordinate coordinate:coords) {
                   
				   //set NADIR to waypoint mode from any other mode
					commandArray.put(new JSONObject().put("device", "23").put("code", "3003").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3003").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3003").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3003").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3003").put("delay", "0").put("activate", "1"));
				   //select waypoint 1-9, does not have increment button
					commandArray.put(new JSONObject().put("device", "23").put("code", "3010").put("delay", "0").put("activate", "1"));
					//press enter
					commandArray.put(new JSONObject().put("device", "23").put("code", "3004").put("delay", "0").put("activate", "1"));
					//press delete x7 to clear longitude
					commandArray.put(new JSONObject().put("device", "23").put("code", "3023").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3023").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3023").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3023").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3023").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3023").put("delay", "0").put("activate", "1"));
					commandArray.put(new JSONObject().put("device", "23").put("code", "3023").put("delay", "0").put("activate", "1"));
					//press 2 for North
					commandArray.put(new JSONObject().put("device", "23").put("code", "3011").put("delay", "0").put("activate", "1"));
            //start entering first 5 latitude digits ***************************PLEASE HELP*****************************************************************************      eg N12'34.5
            String first4Lat = coordinate.getLatitude().replace(".", "").substring(0, 4);
            String last4Lat = coordinate.getLatitude().replace(".", "").substring(4);
            for(char digit:first4Lat.toCharArray()){
                switch (digit){
                    case '1':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3010").put("delay", "0").put("activate", "1"));
                        break;
                    case '2':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3011").put("delay", "0").put("activate", "1"));
                        break;
                    case '3':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3012").put("delay", "0").put("activate", "1"));
                        break;
                    case '4':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3013").put("delay", "0").put("activate", "1"));
                        break;
                    case '5':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3014").put("delay", "0").put("activate", "1"));
                        break;
                    case '6':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3015").put("delay", "0").put("activate", "1"));
                        break;
                    case '7':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3016").put("delay", "0").put("activate", "1"));
                        break;
                    case '8':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3017").put("delay", "0").put("activate", "1"));
                        break;
                    case '9':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3018").put("delay", "0").put("activate", "1"));
                        break;
                    case '0':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3009").put("delay", "0").put("activate", "1"));
                        break;
                }
            }

            //press NADIR down arrow
					commandArray.put(new JSONObject().put("device", "23").put("code", "3008").put("delay", "0").put("activate", "1"));
			//press E east
            commandArray.put(new JSONObject().put("device", "23").put("code", "3015").put("delay", "0").put("activate", "1"));
            //start entering first 5 longitude digits for Caucasus, Syria, Persian Gulf, 6 digits for Nevada, Marianas (WTF!)*******************************************************************
            String first4Long = coordinate.getLongitude().replace(".", "").substring(0, 4);
            String last4Long = coordinate.getLongitude().replace(".", "").substring(4);
            for(char digit:first4Long.toCharArray()){
                switch (digit){
                    case '1':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3010").put("delay", "0").put("activate", "1"));
                        break;
                    case '2':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3011").put("delay", "0").put("activate", "1"));
                        break;
                    case '3':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3012").put("delay", "0").put("activate", "1"));
                        break;
                    case '4':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3013").put("delay", "0").put("activate", "1"));
                        break;
                    case '5':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3014").put("delay", "0").put("activate", "1"));
                        break;
                    case '6':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3015").put("delay", "0").put("activate", "1"));
                        break;
                    case '7':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3016").put("delay", "0").put("activate", "1"));
                        break;
                    case '8':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3017").put("delay", "0").put("activate", "1"));
                        break;
                    case '9':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3018").put("delay", "0").put("activate", "1"));
                        break;
                    case '0':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3009").put("delay", "0").put("activate", "1"));
                        break;
                }
            }
            
            //press enter
            commandArray.put(new JSONObject().put("device", "23").put("code", "3004").put("delay", "0").put("activate", "1"));
        }

        return commandArray;
    }
}
