/*
 * This file is part of NucleusFramework for Bukkit, licensed under the MIT License (MIT).
 *
 * Copyright (c) JCThePants (www.jcwhatever.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.jcwhatever.nucleus.utils.text.dynamic;

import com.jcwhatever.nucleus.utils.PreCon;
import com.jcwhatever.nucleus.utils.text.TextUtils;

/**
 * Dynamic text generated by {@code DynamicTextBuilder}.
 */
public final class DynamicTextComposite implements IDynamicText {

    private final String _template;
    private final IDynamicText[] _args;
    private final int _interval;

    /**
     * Constructor.
     *
     * @param template  Text template.
     * @param interval  A hard set interval. 0 to use dynamic interval.
     * @param args      Format arguments.
     */
    DynamicTextComposite(String template, int interval, IDynamicText... args) {
        PreCon.notNull(template);
        PreCon.notNull(args);

        _template = template;
        _interval = interval;
        _args = args;
    }

    @Override
    public String nextText() {
        return TextUtils.format(_template, _args);
    }

    @Override
    public int getRefreshRate() {

        // check for interval hard set or no dynamic text
        if (_interval > 0 || _args.length == 0)
            return _interval;

        int interval = Integer.MAX_VALUE;

        // get smallest refresh rate required
        for (IDynamicText dyn : _args) {
            if (dyn.getRefreshRate() <= 0)
                continue;

            interval = Math.min(dyn.getRefreshRate(), interval);
        }

        // check for changed
        if (interval == Integer.MAX_VALUE)
            interval = 0;

        return interval;
    }

    @Override
    public String toString() {
        return nextText();
    }
}

@@ -1572,24 +1572,64 @@
 xt%5B%5D _args;%0A
+    private final Object%5B%5D _formatArgs;%0A
     private 
@@ -2069,24 +2069,135 @@
 rgs = args;%0A
+        _formatArgs = new Object%5Bargs.length%5D;%0A        System.arraycopy(args, 0, _formatArgs, 0, args.length);%0A
     %7D%0A%0A    @
@@ -2280,17 +2280,23 @@
 plate, _
-a
+formatA
 rgs);%0A  

fix incorrect array type