package jxl.write.biff;

import java.util.ArrayList;
import java.util.Iterator;
import jxl.biff.IntegerHelper;
import jxl.biff.Type;
import jxl.biff.WritableRecordData;

/* JADX INFO: loaded from: classes.dex */
public class ExternalSheetRecord extends WritableRecordData {
    private byte[] data;
    private ArrayList xtis;

    public static class XTI {
        public int firstTab;
        public int lastTab;
        public int supbookIndex;

        public XTI(int i2, int i3, int i4) {
            this.supbookIndex = i2;
            this.firstTab = i3;
            this.lastTab = i4;
        }

        public void sheetInserted(int i2) {
            int i3 = this.firstTab;
            if (i3 >= i2) {
                this.firstTab = i3 + 1;
            }
            int i4 = this.lastTab;
            if (i4 >= i2) {
                this.lastTab = i4 + 1;
            }
        }

        public void sheetRemoved(int i2) {
            if (this.firstTab == i2) {
                this.firstTab = 0;
            }
            if (this.lastTab == i2) {
                this.lastTab = 0;
            }
            int i3 = this.firstTab;
            if (i3 > i2) {
                this.firstTab = i3 - 1;
            }
            int i4 = this.lastTab;
            if (i4 > i2) {
                this.lastTab = i4 - 1;
            }
        }
    }

    public ExternalSheetRecord(jxl.read.biff.ExternalSheetRecord externalSheetRecord) {
        super(Type.EXTERNSHEET);
        this.xtis = new ArrayList(externalSheetRecord.getNumRecords());
        for (int i2 = 0; i2 < externalSheetRecord.getNumRecords(); i2++) {
            this.xtis.add(new XTI(externalSheetRecord.getSupbookIndex(i2), externalSheetRecord.getFirstTabIndex(i2), externalSheetRecord.getLastTabIndex(i2)));
        }
    }

    @Override // jxl.biff.WritableRecordData
    public byte[] getData() {
        int i2 = 2;
        byte[] bArr = new byte[(this.xtis.size() * 6) + 2];
        IntegerHelper.getTwoBytes(this.xtis.size(), bArr, 0);
        for (XTI xti : this.xtis) {
            IntegerHelper.getTwoBytes(xti.supbookIndex, bArr, i2);
            IntegerHelper.getTwoBytes(xti.firstTab, bArr, i2 + 2);
            IntegerHelper.getTwoBytes(xti.lastTab, bArr, i2 + 4);
            i2 += 6;
        }
        return bArr;
    }

    public int getFirstTabIndex(int i2) {
        return ((XTI) this.xtis.get(i2)).firstTab;
    }

    public int getIndex(int i2, int i3) {
        Iterator it = this.xtis.iterator();
        boolean z = false;
        int i4 = 0;
        while (it.hasNext() && !z) {
            XTI xti = (XTI) it.next();
            if (xti.supbookIndex == i2 && xti.firstTab == i3) {
                z = true;
            } else {
                i4++;
            }
        }
        if (z) {
            return i4;
        }
        this.xtis.add(new XTI(i2, i3, i3));
        return this.xtis.size() - 1;
    }

    public int getLastTabIndex(int i2) {
        return ((XTI) this.xtis.get(i2)).lastTab;
    }

    public int getSupbookIndex(int i2) {
        return ((XTI) this.xtis.get(i2)).supbookIndex;
    }

    public void sheetInserted(int i2) {
        Iterator it = this.xtis.iterator();
        while (it.hasNext()) {
            ((XTI) it.next()).sheetInserted(i2);
        }
    }

    public void sheetRemoved(int i2) {
        Iterator it = this.xtis.iterator();
        while (it.hasNext()) {
            ((XTI) it.next()).sheetRemoved(i2);
        }
    }

    public ExternalSheetRecord() {
        super(Type.EXTERNSHEET);
        this.xtis = new ArrayList();
    }
}
