package main.Waypoints.PlanesCommands;

import main.Utils.CoordinateUtils;
import main.Utils.UnitConvertorUtils;
import main.models.DMMCoordinate;
import main.models.Hemisphere;
import main.models.Point;
import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class AH64 {
    public static JSONArray getCommands(List<Point> coords){
        /*
           PLT_MPD_R_TSD", 43, 3029, 77, "PLT MPD Right", "Pilot Right MPD TSD Button
           PLT_MPD_R_B6", 43, 3013, 71, "PLT MPD Right", "Pilot Right MPD B6 Button
           PLT_MPD_R_L2", 43, 3023, 50, "PLT MPD Right", "Pilot Right MPD L2 Button
           PLT_MPD_R_L1", 43, 3024, 51, "PLT MPD Right", "Pilot Right MPD L1 Button
		   PLT_MPD_R_T1", 43, 3001, 54, "PLT MPD Right", "Pilot Right MPD T1 Button
		   PLT_MPD_R_T5", 43, 3005, 58, "PLT MPD Right", "Pilot Right MPD T5 Button
		   PLT_KU_ENT", 29, 3006, 261, "PLT Keyboard Unit", "Pilot Keyboard Unit ENTER Key
           PLT_KU_CLR", 29, 3001, 258, "PLT Keyboard Unit", "Pilot Keyboard Unit CLR Key
           PLT_KU_N", 29, 3020, 232, "PLT Keyboard Unit", "Pilot Keyboard Unit N Key
		   PLT_KU_S", 29, 3025, 240, "PLT Keyboard Unit", "Pilot Keyboard Unit S Key
		   PLT_KU_E", 29, 3011, 217, "PLT Keyboard Unit", "Pilot Keyboard Unit E Key
		   PLT_KU_W", 29, 3029, 244, "PLT Keyboard Unit", "Pilot Keyboard Unit W Key
           PLT_KU_0", 29, 3043, 247, "PLT Keyboard Unit", "Pilot Keyboard Unit 0 Key
		   PLT_KU_1", 29, 3033, 219, "PLT Keyboard Unit", "Pilot Keyboard Unit 1 Key
		   PLT_KU_2", 29, 3034, 220, "PLT Keyboard Unit", "Pilot Keyboard Unit 2 Key
		   PLT_KU_3", 29, 3035, 221, "PLT Keyboard Unit", "Pilot Keyboard Unit 3 Key
		   PLT_KU_4", 29, 3036, 228, "PLT Keyboard Unit", "Pilot Keyboard Unit 4 Key
		   PLT_KU_5", 29, 3037, 229, "PLT Keyboard Unit", "Pilot Keyboard Unit 5 Key
		   PLT_KU_6", 29, 3038, 230, "PLT Keyboard Unit", "Pilot Keyboard Unit 6 Key
		   PLT_KU_7", 29, 3039, 237, "PLT Keyboard Unit", "Pilot Keyboard Unit 7 Key
		   PLT_KU_8", 29, 3040, 238, "PLT Keyboard Unit", "Pilot Keyboard Unit 8 Key
		   PLT_KU_9", 29, 3041, 239, "PLT Keyboard Unit", "Pilot Keyboard Unit 9 Key
         */

        JSONArray commandArray = new JSONArray();

        
        for (Point coordinate:coords) {
                //TSD Page
        commandArray.put(new JSONObject().put("device", "43").put("code", "3029").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
        //goto Point page
        commandArray.put(new JSONObject().put("device", "43").put("code", "3013").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
		//press ADD button
        commandArray.put(new JSONObject().put("device", "43").put("code", "3023").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
		//press IDENT button
        commandArray.put(new JSONObject().put("device", "43").put("code", "3024").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
		//press KU ENTER  button
        commandArray.put(new JSONObject().put("device", "29").put("code", "3006").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
		//press KU ENTER  button
        commandArray.put(new JSONObject().put("device", "29").put("code", "3006").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
		//press KU CLR button
        commandArray.put(new JSONObject().put("device", "29").put("code", "3001").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
				//check if latitude is N or S
            if(coordinate.getLatitudeHemisphere()== Hemisphere.NORTH){
                //press N
                commandArray.put(new JSONObject().put("device", "29").put("code", "3020").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
            } else {
                //press S
                commandArray.put(new JSONObject().put("device", "29").put("code", "3025").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
            }
            //start typing latitude
            for(char digit:coordinate.getLatitude().toCharArray()){
                switch (digit){
                    case '1':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3033").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '2':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3034").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '3':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3035").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '4':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3036").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '5':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3037").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '6':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3038").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '7':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3039").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '8':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3040").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '9':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3041").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '0':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3043").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                }
            }
            
            //check if longitude is E or W
            if(coordinate.getLongitudeHemisphere()== Hemisphere.EAST){
                //press E
                commandArray.put(new JSONObject().put("device", "29").put("code", "3011").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
            } else {
                //press W
                commandArray.put(new JSONObject().put("device", "29").put("code", "3029").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
            }
            //start typing longitude
            for(char digit:coordinate.getLongitude().toCharArray()){
                switch (digit){
                    case '1':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3033").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '2':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3034").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '3':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3035").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '4':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3036").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '5':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3037").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '6':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3038").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '7':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3039").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '8':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3040").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '9':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3041").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                    case '0':
                        commandArray.put(new JSONObject().put("device", "29").put("code", "3043").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
                        break;
                }
            }
            
			//press KU ENTER button - accept coordinates
			commandArray.put(new JSONObject().put("device", "29").put("code", "3006").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
			//press KU ENTER button - accept altitude
			commandArray.put(new JSONObject().put("device", "29").put("code", "3006").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
			
			//TSD Page
			commandArray.put(new JSONObject().put("device", "43").put("code", "3029").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
			//goto Coord page
			commandArray.put(new JSONObject().put("device", "43").put("code", "3005").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
            //goto WP page
			commandArray.put(new JSONObject().put("device", "43").put("code", "3001").put("delay", "05").put("activate", "1").put("addDepress", "true").put("delay", "05"));
        }
        
        return commandArray;
    }

    public static List<Point> getCoords(List<Point> dcsPoints){
        List<Point> ah64Points = new ArrayList<>();
        for (Point dcsPoint:dcsPoints){
            BigDecimal dcsLat = new BigDecimal(dcsPoint.getLatitude());
            BigDecimal dcsLong = new BigDecimal(dcsPoint.getLongitude());
            Double dcsElev = Double.parseDouble(dcsPoint.getElevation());

            DMMCoordinate dmmLat = CoordinateUtils.decimalToDMM(dcsLat);
            DMMCoordinate dmmLong = CoordinateUtils.decimalToDMM(dcsLong);

            DecimalFormat latDegDf = new DecimalFormat("00");
            DecimalFormat latMinDf = new DecimalFormat("00.00");
            DecimalFormat longDegDf = new DecimalFormat("000");
            DecimalFormat longMinDf = new DecimalFormat("00.00");
            String ah64Latitude = latDegDf.format(dmmLat.getDegrees())+latMinDf.format(dmmLat.getMinutes()).replace(".", "");
            String ah64Longitude = longDegDf.format(dmmLong.getDegrees())+longMinDf.format(dmmLong.getMinutes()).replace(".", "");
            String ah64Elevation = String.valueOf(Math.round(UnitConvertorUtils.metersToFeet(dcsElev)));

            var ah64Point = new Point(ah64Latitude, ah64Longitude, ah64Elevation, dcsPoint.getLatitudeHemisphere(), dcsPoint.getLongitudeHemisphere());
            ah64Points.add(ah64Point);
        }
        return ah64Points;
    }
}
