/*
 * Copyright (c) 2015. Marvin Hofmann <marvin@marv-productions.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package de.marvprod.yabon.token;

import de.marvprod.yabon.Encoder;

import java.math.BigInteger;

public class BigIntToken extends BinaryToken {

    public BigIntToken(byte[] data) {
        super(data);
    }

    public BigIntToken(long data) {
        super(BigInteger.valueOf(data).toByteArray());
    }

    public BigIntToken(BigInteger data) {
        super(data.toByteArray());
    }

    public TokenCategories getTokenCategory() {
        return TokenCategories.NUMBER_BIGINT;
    }

    @Override
    public String explain(String level) {
        byte[] enc = toBinary();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\n")
                .append(Encoder.byteToString(enc[0]))
                .append(" ")
                .append(level)
                .append("BIGNIT")
                .append("(")
                .append(new BigInteger(data).toString())
                .append(") length ")
                .append(data.length)
                .append(" byte size ")
                .append(size(data.length))
                .append(" byte");
        for (int i = 1; i < size(data.length) + 1; i++) {
            stringBuilder.append("\n")
                    .append(Encoder.byteToString(enc[i]))
                    .append(" ")
                    .append(level).append(LEVEL)
                    .append("BIGNIT")
                    .append(" length byte ")
                    .append(i);
        }

        for (int i = 1 + size(data.length); i < enc.length; i++) {
            stringBuilder.append("\n")
                    .append(Encoder.byteToString(enc[i]))
                    .append(" ")
                    .append(level).append(LEVEL)
                    .append("BIGNIT")
                    .append(" byte ")
                    .append(i);
        }
        return stringBuilder.toString();
    }

}
