package hws.hw4;

/**
 * There are three fencers in a melee. Each fencer fights every other fencer, so
 * there are three bouts in total. What makes it interesting is that the phrases
 * in the bouts are interleaved by round.
 *
 * @author CS159 Faculty
 * @version 02/24/2025
 */
public class MeleeDriver {

    /**
     * In each round, each fencer fights one phrase with another fencer using a
     * particular weapon (that is chosen at the start of the melee). When that
     * phrase concludes, one of the two fencers moves on to the next bout.
     *
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        Bout bernVstew = new Bout(FencingEvent.EPEE, "Bernstein", "Stewart");
        Bout bernVduan = new Bout(FencingEvent.SABRE, "Bernstein", "Duan");
        Bout duanVstew = new Bout(FencingEvent.FOIL, "Duan", "Stewart");

        // Round 1
        processPhrase(bernVstew, "Bernstein", BladePart.TIP, BodyPart.LEGS);
        processPhrase(bernVduan, "Duan", BladePart.EDGE, BodyPart.HEAD);
        processPhrase(duanVstew, "Duan", BladePart.TIP, BodyPart.ARMS);

        // Round 2
        processPhrase(bernVstew, "Stewart", BladePart.TIP, BodyPart.HEAD);
        processPhrase(bernVduan, "Duan", BladePart.EDGE, BodyPart.HEAD);
        processPhrase(duanVstew, "Stewart", BladePart.TIP, BodyPart.ARMS);

        // Round 3
        processPhrase(bernVstew, "Bernstein", BladePart.EDGE, BodyPart.ARMS);
        processPhrase(bernVduan, "Duan", BladePart.EDGE, BodyPart.LEGS);
        processPhrase(duanVstew, "Stewart", BladePart.TIP, BodyPart.HANDS);

        // Round 4
        processPhrase(bernVstew, "Stewart", BladePart.TIP, BodyPart.TORSO);
        processPhrase(bernVduan, "Duan", BladePart.EDGE, BodyPart.ARMS);
        processPhrase(duanVstew, "Duan", BladePart.TIP, BodyPart.TORSO);

        // Print the scores after four rounds of the three bouts
        System.out.println(bernVstew.toString());
        System.out.println(bernVduan.toString());
        System.out.println(duanVstew.toString());
    }

    /**
     * Process a phrase in a bout to determine if a point was scored.
     *
     * @param bout The bout
     * @param offensiveFencer The fencer that was on offense
     * @param bladePart The part of the blade that contacted the defensive
     * fencer
     * @param bodyPart The part of the body on the defensive fencer that was
     * contacted
     */
    private static void processPhrase(Bout bout, String offensiveFencer,
            BladePart bladePart, BodyPart bodyPart) {

        FencingEvent event = bout.getEvent();
        if (event.isScoringTouch(bladePart, bodyPart)) {
            if (offensiveFencer.equals(bout.getLeftFencer())) {
                bout.increaseLeftScore();
            } else {
                bout.increaseRightScore();
            }
        }
    }
}
