/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.impressremote.communication;

import java.util.Random;

import android.text.TextUtils;

final class Protocol {
    private Protocol() {
    }

    public static final String CHARSET = "UTF-8";

    public static final class Pin {
        private Pin() {
        }

        private static final int NUMBERS_COUNT = 4;

        public static String generate() {
            Random aRandomGenerator = new Random();
            int aMaximumPin = (int) Math.pow(10, NUMBERS_COUNT) - 1;
            int aPinNumber = aRandomGenerator.nextInt(aMaximumPin);

            return String.format("%04d", aPinNumber);
        }
    }

    public static final class Ports {
        private Ports() {
        }

        public static final int SERVER_SEARCH = 1598;
        public static final int CLIENT_CONNECTION = 1599;
    }

    public static final class Addresses {
        private Addresses() {
        }

        public static final String SERVER_SEARCH = "239.0.0.1";
    }

    public static final class Messages {
        private Messages() {
        }

        public static final String PAIRED = "LO_SERVER_SERVER_PAIRED";
        public static final String VALIDATING = "LO_SERVER_VALIDATING_PIN";
        public static final String ADVERTISE = "LOREMOTE_ADVERTISE";

        public static final String SLIDE_SHOW_STARTED = "slideshow_started";
        public static final String SLIDE_SHOW_FINISHED = "slideshow_finished";
        public static final String SLIDE_UPDATED = "slide_updated";
        public static final String SLIDE_PREVIEW = "slide_preview";
        public static final String SLIDE_NOTES = "slide_notes";
    }

    public static final class Commands {
        private Commands() {
        }

        public static final String PAIR_WITH_SERVER = "LO_SERVER_CLIENT_PAIR";
        public static final String SEARCH_SERVERS = "LOREMOTE_SEARCH";

        public static final String TRANSITION_NEXT = "transition_next";
        public static final String TRANSITION_PREVIOUS = "transition_previous";
        public static final String GO_TO_SLIDE = "goto_slide";
        public static final String PRESENTATION_BLANK_SCREEN = "presentation_blank_screen";
        public static final String PRESENTATION_RESUME = "presentation_resume";
        public static final String PRESENTATION_START = "presentation_start";
        public static final String PRESENTATION_STOP = "presentation_stop";

        private static final String DELIMITER_PARAMETER = "\n";
        private static final String DELIMITER_COMMAND = "\n\n";

        public static String prepareCommand(String aCommand) {
            return String.format("%s%s", aCommand, DELIMITER_COMMAND);
        }

        public static String prepareCommand(String... aParameters) {
            String aCommand = TextUtils.join(DELIMITER_PARAMETER, aParameters);

            return prepareCommand(aCommand);
        }
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

@@ -557,435 +557,8 @@
 %22;%0A%0A
-    public static final class Pin %7B%0A        private Pin() %7B%0A        %7D%0A%0A        private static final int NUMBERS_COUNT = 4;%0A%0A        public static String generate() %7B%0A            Random aRandomGenerator = new Random();%0A            int aMaximumPin = (int) Math.pow(10, NUMBERS_COUNT) - 1;%0A            int aPinNumber = aRandomGenerator.nextInt(aMaximumPin);%0A%0A            return String.format(%22%2504d%22, aPinNumber);%0A        %7D%0A    %7D%0A%0A
     
@@ -2820,16 +2820,1162 @@
  %7D%0A    %7D
+%0A%0A    public static final class Pin %7B%0A        private static final int NUMBERS_COUNT = 4;%0A%0A        private Pin() %7B%0A        %7D%0A%0A        public static String generate() %7B%0A            return new Pin().generatePinText();%0A        %7D%0A%0A        private String generatePinText() %7B%0A            int aPin = preventLeadingZeros(generatePinNumber());%0A%0A            return String.format(buildPinFormat(), aPin);%0A        %7D%0A%0A        private int generatePinNumber() %7B%0A            int aMaximumPin = (int) Math.pow(10, NUMBERS_COUNT) - 1;%0A%0A            return new Random().nextInt(aMaximumPin);%0A        %7D%0A%0A        private int preventLeadingZeros(int aPin) %7B%0A            // Pin cannot have leading zeros.%0A            // LibreOffice Impress doesn%E2%80%99t allow to enter leading zeros.%0A            // Bug exists at least at LibreOffice 4.1.%0A%0A            int aMinimumPin = (int) Math.pow(10, NUMBERS_COUNT - 1);%0A%0A            if (aPin %3E= aMinimumPin) %7B%0A                return aPin;%0A            %7D%0A%0A            return aPin + aMinimumPin;%0A        %7D%0A%0A        private String buildPinFormat() %7B%0A            return String.format(%22%25%250%25sd%22, Integer.toString(NUMBERS_COUNT));%0A        %7D%0A    %7D
 %0A%7D%0A%0A/* v

Fix possible pin-related bug.