001/************************ PROJECT ZIXI ************************/
002/* Copyright (c) 2026 StuyPulse Robotics. All rights reserved.*/
003/* This work is licensed under the terms of the MIT license.  */
004/**************************************************************/
005package com.stuypulse.robot.util.auton;
006
007import com.stuypulse.robot.commands.auton.Auton;
008import com.stuypulse.robot.commands.auton.BLineAuton;
009import com.stuypulse.robot.opmodes.AutonOpMode;
010import com.stuypulse.robot.util.auton.AutonUtil.AutonType;
011
012import org.wpilib.driverstation.DriverStationErrors;
013
014import bline.path.Path;
015import java.util.function.Function;
016import org.littletonrobotics.junction.networktables.LoggedNetworkChooser;
017
018/**
019 *
020 *
021 * <h2>BLine Utility</h2>
022 *
023 * <p>Loads BLine path (.json) files from the deploy directory and builds named autonomous routines
024 * from those paths.
025 *
026 * <h3>Usage</h3>
027 *
028 * <p>For example, inside {@link com.stuypulse.robot.Robot#configureAutons()}, you can create a
029 * {@link BLineConfig}, pass in the auton name, factory method, and path names, then register it
030 * with {@link AutonOpMode} via {@link BLineConfig#register()}.
031 *
032 * <pre>{@code
033 * AutonFactory factory = new AutonFactory(swerve);
034 *
035 * BLineConfig scoreAutonConfig = new BLineConfig(
036 *                 "Score",
037 *                 factory::score,
038 *                 "Score Path");
039 *
040 * scoreAutonConfig.register();
041 * }</pre>
042 *
043 * <p>The factory method receives the loaded paths in the order listed and returns a {@link
044 * BLineAuton}. Use {@link BLineConfig#register()} for a regular option, or {@link
045 * BLineConfig#registerDefault()} for the chooser's default option. A config can also register
046 * directly with a {@link LoggedNetworkChooser} when a custom chooser is used. The methods without a
047 * chooser register the routine with {@link AutonOpMode}.
048 *
049 * <p>Path names are checked when the configuration is created. Missing paths are reported to the
050 * Driver Station with a suggested closest match when one is available.
051 *
052 * @author Taryn Huang (https://github.com/7arymisu)
053 */
054public final class BLineUtil {
055    private BLineUtil() {
056        throw new UnsupportedOperationException(
057                "This is a utility class and cannot be instantiated");
058    }
059
060    /**
061     *
062     *
063     * <h2>BLine Configuration</h2>
064     *
065     * <p>Associates an autonomous routine name and one or more BLine path names with a factory
066     * method that creates the corresponding {@link BLineAuton}. Create one configuration for each
067     * routine, then call one of the {@code register} methods to add it to an autonomous chooser.
068     */
069    public static class BLineConfig {
070
071        private final String name;
072
073        private final Function<Path[], BLineAuton> auton;
074
075        private final String[] paths;
076
077        /**
078         *
079         *
080         * <h4>Constructor for a BLineConfig</h4>
081         *
082         * <p>Stores the routine name, path names, and factory. Checks if every path exists and
083         * reports missing paths and suggested closest matches to the Driver Station.
084         *
085         * @param name The name to display for this autonomous routine
086         * @param auton Factory that creates the routine from the loaded paths
087         * @param paths BLine path names used by the routine, in the order expected by the factory
088         */
089        public BLineConfig(String name, Function<Path[], BLineAuton> auton, String... paths) {
090            this.name = name;
091            this.auton = auton;
092            this.paths = paths;
093
094            for (String path : paths) {
095                try {
096                    new Path(path);
097                } catch (Exception e) {
098                    DriverStationErrors.reportError(
099                            "BLine path \""
100                                    + path
101                                    + "\" not found. Did you mean \""
102                                    + AutonUtil.findClosestMatch(
103                                            AutonUtil.getPathFileNames(AutonType.BLINE), path)
104                                    + "\"?",
105                            false);
106                }
107            }
108        }
109
110        /**
111         * Loads the configured paths and passes them to the routine factory.
112         *
113         * @return The autonomous routine created from the configured paths
114         */
115        private BLineAuton buildBLineAuton() {
116            return auton.apply(loadPaths(paths)); // pass names straight through, no pre-loading
117        }
118
119        /**
120         * Adds this routine to a {@link LoggedNetworkChooser} under the configured name.
121         *
122         * @param chooser The chooser that will display this routine
123         * @return This instance of the class, for method chaining
124         */
125        public BLineConfig register(LoggedNetworkChooser<Auton> chooser) {
126            chooser.add(name, buildBLineAuton());
127            return this;
128        }
129
130        /**
131         * Registers this routine with {@link AutonOpMode}'s internal chooser.
132         *
133         * @return This instance of the class, for method chaining
134         */
135        public BLineConfig register() {
136            AutonOpMode.addAuton(name, buildBLineAuton());
137            return this;
138        }
139
140        /**
141         * Adds this routine as the default option in a {@link LoggedNetworkChooser}.
142         *
143         * @param chooser The chooser that will display this routine
144         * @return This instance of the class, for method chaining
145         */
146        public BLineConfig registerDefault(LoggedNetworkChooser<Auton> chooser) {
147            chooser.addDefault(name, buildBLineAuton());
148            return this;
149        }
150
151        /**
152         * Registers this routine as the default with {@link AutonOpMode}'s internal chooser.
153         *
154         * @return This instance of the class, for method chaining
155         */
156        public BLineConfig registerDefault() {
157            AutonOpMode.addDefaultAuton(name, buildBLineAuton());
158            return this;
159        }
160    }
161
162    // PATH LOADING METHODS
163
164    /**
165     * Loads multiple BLine paths in the order given. Each name is passed to {@link #load(String)}.
166     *
167     * <p>If a path cannot be loaded or found, an error is reported to the Driver Station, and null
168     * is returned in the corresponding position.
169     *
170     * @param names BLine path names to load
171     * @return The corresponding paths where entries that fail to load are {@code null}
172     */
173    public static Path[] loadPaths(String... names) {
174        Path[] output = new Path[names.length];
175        for (int i = 0; i < names.length; i++) {
176            output[i] = load(names[i]);
177        }
178        return output;
179    }
180
181    /**
182     * Loads a BLine path by name.
183     *
184     * <p>If the path cannot be loaded or found, an error is reported to the Driver Station, and
185     * null is returned.
186     *
187     * @param name BLine path name
188     * @return The loaded path, or {@code null} if not found
189     */
190    public static Path load(String name) {
191        try {
192            return new Path(name);
193        } catch (Exception e) {
194            DriverStationErrors.reportError("BLine path \"" + name + "\" not found.", false);
195            return null;
196        }
197    }
198}