package main.Waypoints.PlanesCommands;

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

import java.util.ArrayList;

public class AV8BNA {
    public static JSONArray getCommands(ArrayList<Coordinate> coords){
        /*
           Button List ODU, device 24
		   ODU option 1 3250
		   ODU option 2 3251
		   ODU option 3 3252
		   
		   Left MFD, device 26
		   OBS PB 2 3201
		   OBS PB 18 3217
		   
		  UFC device 23
           1       3302
           2/N     3303
           3       3304
           4       3306
           5       3307
           6/E     3308
           7       3310
           8       3311
           9       3312
           0       3315
           ENTR    3314

         */

        JSONArray commandArray = new JSONArray();


        //rtn to MFD menu page
        commandArray.put(new JSONObject().put("device", "26").put("code", "3201").put("delay", "0").put("activate", "1"));
        //goto EHSD page
        commandArray.put(new JSONObject().put("device", "26").put("code", "3217").put("delay", "0").put("activate", "1"));
		//enter EHSD data mode, WP is pre-selected
        commandArray.put(new JSONObject().put("device", "26").put("code", "3217").put("delay", "0").put("activate", "1"));
		//enter 99 in waypoint field to auto-select next available waypoint
        commandArray.put(new JSONObject().put("device", "23").put("code", "3312").put("delay", "0").put("activate", "1"));
		commandArray.put(new JSONObject().put("device", "23").put("code", "3312").put("delay", "0").put("activate", "1"));
		//press enter to confirm waypoint
        commandArray.put(new JSONObject().put("device", "23").put("code", "3314").put("delay", "0").put("activate", "1"));
        
		for (Coordinate coordinate:coords) {
            //press ODU 2 for POS entry - latitude
            commandArray.put(new JSONObject().put("device", "24").put("code", "3251").put("delay", "0").put("activate", "1"));
            //press N
            commandArray.put(new JSONObject().put("device", "23").put("code", "3303").put("delay", "0").put("activate", "1"));
            //start typing latitude  DD.MM.SS
            for(char digit:coordinate.getLatitude().toCharArray()){
                switch (digit){
                    case '1':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3302").put("delay", "0").put("activate", "1"));
                        break;
                    case '2':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3303").put("delay", "0").put("activate", "1"));
                        break;
                    case '3':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3304").put("delay", "0").put("activate", "1"));
                        break;
                    case '4':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3306").put("delay", "0").put("activate", "1"));
                        break;
                    case '5':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3307").put("delay", "0").put("activate", "1"));
                        break;
                    case '6':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3308").put("delay", "0").put("activate", "1"));
                        break;
                    case '7':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3310").put("delay", "0").put("activate", "1"));
                        break;
                    case '8':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3311").put("delay", "0").put("activate", "1"));
                        break;
                    case '9':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3312").put("delay", "0").put("activate", "1"));
                        break;
                    case '0':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3315").put("delay", "0").put("activate", "1"));
                        break;
                }
            }
            //press enter
            commandArray.put(new JSONObject().put("device", "23").put("code", "3314").put("delay", "0").put("activate", "1"));
            //press ODU 2 to change to longitude
            commandArray.put(new JSONObject().put("device", "24").put("code", "3251").put("delay", "0").put("activate", "1"));
            //press E
            commandArray.put(new JSONObject().put("device", "23").put("code", "3308").put("delay", "0").put("activate", "1"));
            //press 0
            commandArray.put(new JSONObject().put("device", "23").put("code", "3315").put("delay", "0").put("activate", "1"));
            //start typing longitude DDD.MM.SS
            for(char digit:coordinate.getLatitude().toCharArray()){
                switch (digit){
                    case '1':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3302").put("delay", "0").put("activate", "1"));
                        break;
                    case '2':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3303").put("delay", "0").put("activate", "1"));
                        break;
                    case '3':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3304").put("delay", "0").put("activate", "1"));
                        break;
                    case '4':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3306").put("delay", "0").put("activate", "1"));
                        break;
                    case '5':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3307").put("delay", "0").put("activate", "1"));
                        break;
                    case '6':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3308").put("delay", "0").put("activate", "1"));
                        break;
                    case '7':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3310").put("delay", "0").put("activate", "1"));
                        break;
                    case '8':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3311").put("delay", "0").put("activate", "1"));
                        break;
                    case '9':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3312").put("delay", "0").put("activate", "1"));
                        break;
                    case '0':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3315").put("delay", "0").put("activate", "1"));
                        break;
                }
            }
            //press enter
            commandArray.put(new JSONObject().put("device", "23").put("code", "3314").put("delay", "0").put("activate", "1"));
            //press ODU 2 to switch back to latitude or next waypoint entry will start with longitude and be incorrect
            commandArray.put(new JSONObject().put("device", "24").put("code", "3251").put("delay", "0").put("activate", "1"));
			
			//goto elevation field ODU 3
            commandArray.put(new JSONObject().put("device", "24").put("code", "3252").put("delay", "0").put("activate", "1"));
            //start entering elevation, feet
            for(char digit:coordinate.getElevation().toCharArray()){
                switch (digit){
                    case '1':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3302").put("delay", "0").put("activate", "1"));
                        break;
                    case '2':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3303").put("delay", "0").put("activate", "1"));
                        break;
                    case '3':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3304").put("delay", "0").put("activate", "1"));
                        break;
                    case '4':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3306").put("delay", "0").put("activate", "1"));
                        break;
                    case '5':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3307").put("delay", "0").put("activate", "1"));
                        break;
                    case '6':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3308").put("delay", "0").put("activate", "1"));
                        break;
                    case '7':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3310").put("delay", "0").put("activate", "1"));
                        break;
                    case '8':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3311").put("delay", "0").put("activate", "1"));
                        break;
                    case '9':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3312").put("delay", "0").put("activate", "1"));
                        break;
                    case '0':
                        commandArray.put(new JSONObject().put("device", "23").put("code", "3315").put("delay", "0").put("activate", "1"));
                        break;
                }
            }
			
			//press ODU 1 to return to waypoint selection mode
			commandArray.put(new JSONObject().put("device", "24").put("code", "3250").put("delay", "0").put("activate", "1"));           
        
        }
        //exit EHSD data mode
        commandArray.put(new JSONObject().put("device", "26").put("code", "3217").put("delay", "0").put("activate", "1"));

        return commandArray;
    }
}
