package jxl.biff;

import common.Assert;
import common.Logger;
import java.io.IOException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import jxl.format.Colour;
import jxl.format.RGB;
import jxl.write.biff.File;

/* JADX INFO: loaded from: classes.dex */
public class FormattingRecords {
    static /* synthetic */ Class class$jxl$biff$FormattingRecords = null;
    private static final int customFormatStartIndex = 164;
    private static Logger logger = null;
    private static final int maxFormatRecordsIndex = 441;
    private static final int minXFRecords = 21;
    private Fonts fonts;
    private PaletteRecord palette;
    private ArrayList xfRecords = new ArrayList(10);
    private HashMap formats = new HashMap(10);
    private ArrayList formatsList = new ArrayList(10);
    private int nextCustomIndexNumber = customFormatStartIndex;

    static {
        Class clsClass$ = class$jxl$biff$FormattingRecords;
        if (clsClass$ == null) {
            clsClass$ = class$("jxl.biff.FormattingRecords");
            class$jxl$biff$FormattingRecords = clsClass$;
        }
        logger = Logger.getLogger(clsClass$);
    }

    public FormattingRecords(Fonts fonts) {
        this.fonts = fonts;
    }

    static /* synthetic */ Class class$(String str) {
        try {
            return Class.forName(str);
        } catch (ClassNotFoundException e) {
            throw new NoClassDefFoundError(e.getMessage());
        }
    }

    public final void addFormat(DisplayFormat displayFormat) throws NumFormatRecordsException {
        if (displayFormat.isInitialized() && displayFormat.getFormatIndex() >= maxFormatRecordsIndex) {
            logger.warn("Format index exceeds Excel maximum - assigning custom number");
            displayFormat.initialize(this.nextCustomIndexNumber);
            this.nextCustomIndexNumber++;
        }
        if (!displayFormat.isInitialized()) {
            displayFormat.initialize(this.nextCustomIndexNumber);
            this.nextCustomIndexNumber++;
        }
        if (this.nextCustomIndexNumber > maxFormatRecordsIndex) {
            this.nextCustomIndexNumber = maxFormatRecordsIndex;
            throw new NumFormatRecordsException();
        }
        if (displayFormat.getFormatIndex() >= this.nextCustomIndexNumber) {
            this.nextCustomIndexNumber = displayFormat.getFormatIndex() + 1;
        }
        if (displayFormat.isBuiltIn()) {
            return;
        }
        this.formatsList.add(displayFormat);
        this.formats.put(new Integer(displayFormat.getFormatIndex()), displayFormat);
    }

    public final void addStyle(XFRecord xFRecord) throws NumFormatRecordsException {
        if (!xFRecord.isInitialized()) {
            xFRecord.initialize(this.xfRecords.size(), this, this.fonts);
            this.xfRecords.add(xFRecord);
        } else if (xFRecord.getXFIndex() >= this.xfRecords.size()) {
            this.xfRecords.add(xFRecord);
        }
    }

    public RGB getColourRGB(Colour colour) {
        PaletteRecord paletteRecord = this.palette;
        return paletteRecord == null ? colour.getDefaultRGB() : paletteRecord.getColourRGB(colour);
    }

    public final DateFormat getDateFormat(int i2) {
        XFRecord xFRecord = (XFRecord) this.xfRecords.get(i2);
        if (xFRecord.isDate()) {
            return xFRecord.getDateFormat();
        }
        FormatRecord formatRecord = (FormatRecord) this.formats.get(new Integer(xFRecord.getFormatRecord()));
        if (formatRecord != null && formatRecord.isDate()) {
            return formatRecord.getDateFormat();
        }
        return null;
    }

    protected final Fonts getFonts() {
        return this.fonts;
    }

    FormatRecord getFormatRecord(int i2) {
        return (FormatRecord) this.formats.get(new Integer(i2));
    }

    public final NumberFormat getNumberFormat(int i2) {
        XFRecord xFRecord = (XFRecord) this.xfRecords.get(i2);
        if (xFRecord.isNumber()) {
            return xFRecord.getNumberFormat();
        }
        FormatRecord formatRecord = (FormatRecord) this.formats.get(new Integer(xFRecord.getFormatRecord()));
        if (formatRecord != null && formatRecord.isNumber()) {
            return formatRecord.getNumberFormat();
        }
        return null;
    }

    protected final int getNumberOfFormatRecords() {
        return this.formatsList.size();
    }

    public PaletteRecord getPalette() {
        return this.palette;
    }

    public final XFRecord getXFRecord(int i2) {
        return (XFRecord) this.xfRecords.get(i2);
    }

    public final boolean isDate(int i2) {
        XFRecord xFRecord = (XFRecord) this.xfRecords.get(i2);
        if (xFRecord.isDate()) {
            return true;
        }
        FormatRecord formatRecord = (FormatRecord) this.formats.get(new Integer(xFRecord.getFormatRecord()));
        if (formatRecord == null) {
            return false;
        }
        return formatRecord.isDate();
    }

    public IndexMapping rationalize(IndexMapping indexMapping, IndexMapping indexMapping2) {
        for (XFRecord xFRecord : this.xfRecords) {
            if (xFRecord.getFormatRecord() >= customFormatStartIndex) {
                xFRecord.setFormatIndex(indexMapping2.getNewIndex(xFRecord.getFormatRecord()));
            }
            xFRecord.setFontIndex(indexMapping.getNewIndex(xFRecord.getFontIndex()));
        }
        ArrayList arrayList = new ArrayList(21);
        IndexMapping indexMapping3 = new IndexMapping(this.xfRecords.size());
        int iMin = Math.min(21, this.xfRecords.size());
        for (int i2 = 0; i2 < iMin; i2++) {
            arrayList.add(this.xfRecords.get(i2));
            indexMapping3.setMapping(i2, i2);
        }
        if (iMin < 21) {
            logger.warn("There are less than the expected minimum number of XF records");
            return indexMapping3;
        }
        int i3 = 0;
        for (int i4 = 21; i4 < this.xfRecords.size(); i4++) {
            XFRecord xFRecord2 = (XFRecord) this.xfRecords.get(i4);
            Iterator it = arrayList.iterator();
            boolean z = false;
            while (it.hasNext() && !z) {
                XFRecord xFRecord3 = (XFRecord) it.next();
                if (xFRecord3.equals(xFRecord2)) {
                    indexMapping3.setMapping(i4, indexMapping3.getNewIndex(xFRecord3.getXFIndex()));
                    i3++;
                    z = true;
                }
            }
            if (!z) {
                arrayList.add(xFRecord2);
                indexMapping3.setMapping(i4, i4 - i3);
            }
        }
        Iterator it2 = this.xfRecords.iterator();
        while (it2.hasNext()) {
            ((XFRecord) it2.next()).rationalize(indexMapping3);
        }
        this.xfRecords = arrayList;
        return indexMapping3;
    }

    public IndexMapping rationalizeDisplayFormats() {
        ArrayList<DisplayFormat> arrayList = new ArrayList();
        IndexMapping indexMapping = new IndexMapping(this.nextCustomIndexNumber);
        int i2 = 0;
        for (DisplayFormat displayFormat : this.formatsList) {
            Assert.verify(!displayFormat.isBuiltIn());
            Iterator it = arrayList.iterator();
            boolean z = false;
            while (it.hasNext() && !z) {
                DisplayFormat displayFormat2 = (DisplayFormat) it.next();
                if (displayFormat2.equals(displayFormat)) {
                    indexMapping.setMapping(displayFormat.getFormatIndex(), indexMapping.getNewIndex(displayFormat2.getFormatIndex()));
                    i2++;
                    z = true;
                }
            }
            if (!z) {
                arrayList.add(displayFormat);
                if (displayFormat.getFormatIndex() - i2 > maxFormatRecordsIndex) {
                    logger.warn("Too many number formats - using default format.");
                }
                indexMapping.setMapping(displayFormat.getFormatIndex(), displayFormat.getFormatIndex() - i2);
            }
        }
        this.formatsList = arrayList;
        for (DisplayFormat displayFormat3 : arrayList) {
            displayFormat3.initialize(indexMapping.getNewIndex(displayFormat3.getFormatIndex()));
        }
        return indexMapping;
    }

    public IndexMapping rationalizeFonts() {
        return this.fonts.rationalize();
    }

    public void setColourRGB(Colour colour, int i2, int i3, int i4) {
        if (this.palette == null) {
            this.palette = new PaletteRecord();
        }
        this.palette.setColourRGB(colour, i2, i3, i4);
    }

    public void setPalette(PaletteRecord paletteRecord) {
        this.palette = paletteRecord;
    }

    public void write(File file) throws IOException {
        Iterator it = this.formatsList.iterator();
        while (it.hasNext()) {
            file.write((FormatRecord) it.next());
        }
        Iterator it2 = this.xfRecords.iterator();
        while (it2.hasNext()) {
            file.write((XFRecord) it2.next());
        }
        file.write(new BuiltInStyle(16, 3));
        file.write(new BuiltInStyle(17, 6));
        file.write(new BuiltInStyle(18, 4));
        file.write(new BuiltInStyle(19, 7));
        file.write(new BuiltInStyle(0, 0));
        file.write(new BuiltInStyle(20, 5));
    }
}
