package jxl.write.biff;

import com.seewo.sdk.model.SDKKeyboardCode;
import common.Assert;
import common.Logger;
import jxl.WorkbookSettings;
import jxl.biff.EncodedURLHelper;
import jxl.biff.IntegerHelper;
import jxl.biff.StringHelper;
import jxl.biff.Type;
import jxl.biff.WritableRecordData;

/* JADX INFO: loaded from: classes.dex */
class SupbookRecord extends WritableRecordData {
    public static final SupbookType ADDIN;
    public static final SupbookType EXTERNAL;
    public static final SupbookType INTERNAL;
    public static final SupbookType LINK;
    public static final SupbookType UNKNOWN;
    static /* synthetic */ Class class$jxl$write$biff$SupbookRecord;
    private static Logger logger;
    private byte[] data;
    private String fileName;
    private int numSheets;
    private String[] sheetNames;
    private SupbookType type;
    private WorkbookSettings workbookSettings;

    /* JADX INFO: Access modifiers changed from: private */
    static class SupbookType {
        private SupbookType() {
        }
    }

    static {
        Class clsClass$ = class$jxl$write$biff$SupbookRecord;
        if (clsClass$ == null) {
            clsClass$ = class$("jxl.write.biff.SupbookRecord");
            class$jxl$write$biff$SupbookRecord = clsClass$;
        }
        logger = Logger.getLogger(clsClass$);
        INTERNAL = new SupbookType();
        EXTERNAL = new SupbookType();
        ADDIN = new SupbookType();
        LINK = new SupbookType();
        UNKNOWN = new SupbookType();
    }

    public SupbookRecord() {
        super(Type.SUPBOOK);
        this.type = ADDIN;
        try {
            throw new Exception();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    private void initAddin() {
        this.data = new byte[]{1, 0, 1, SDKKeyboardCode.FUNCTION_KEY_BOARD_F1};
    }

    private void initExternal() {
        int i2 = 0;
        int length = 0;
        for (int i3 = 0; i3 < this.numSheets; i3++) {
            length += this.sheetNames[i3].length();
        }
        byte[] encodedURL = EncodedURLHelper.getEncodedURL(this.fileName, this.workbookSettings);
        int length2 = encodedURL.length + 6;
        int i4 = this.numSheets;
        byte[] bArr = new byte[length2 + (i4 * 3) + (length * 2)];
        this.data = bArr;
        IntegerHelper.getTwoBytes(i4, bArr, 0);
        IntegerHelper.getTwoBytes(encodedURL.length + 1, this.data, 2);
        byte[] bArr2 = this.data;
        bArr2[4] = 0;
        bArr2[5] = 1;
        System.arraycopy(encodedURL, 0, bArr2, 6, encodedURL.length);
        int length3 = encodedURL.length + 4 + 2;
        while (true) {
            String[] strArr = this.sheetNames;
            if (i2 >= strArr.length) {
                return;
            }
            IntegerHelper.getTwoBytes(strArr[i2].length(), this.data, length3);
            byte[] bArr3 = this.data;
            bArr3[length3 + 2] = 1;
            StringHelper.getUnicodeBytes(this.sheetNames[i2], bArr3, length3 + 3);
            length3 += (this.sheetNames[i2].length() * 2) + 3;
            i2++;
        }
    }

    private void initInternal(jxl.read.biff.SupbookRecord supbookRecord) {
        this.numSheets = supbookRecord.getNumberOfSheets();
        initInternal();
    }

    void adjustInternal(int i2) {
        Assert.verify(this.type == INTERNAL);
        this.numSheets = i2;
        initInternal();
    }

    @Override // jxl.biff.WritableRecordData
    public byte[] getData() {
        SupbookType supbookType = this.type;
        if (supbookType == INTERNAL) {
            initInternal();
        } else if (supbookType == EXTERNAL) {
            initExternal();
        } else if (supbookType == ADDIN) {
            initAddin();
        } else {
            logger.warn("unsupported supbook type - defaulting to internal");
            initInternal();
        }
        return this.data;
    }

    public String getFileName() {
        return this.fileName;
    }

    public int getNumberOfSheets() {
        return this.numSheets;
    }

    public int getSheetIndex(String str) {
        int i2 = 0;
        boolean z = false;
        while (true) {
            String[] strArr = this.sheetNames;
            if (i2 >= strArr.length || z) {
                break;
            }
            if (strArr[i2].equals(str)) {
                z = true;
            }
            i2++;
        }
        if (z) {
            return 0;
        }
        String[] strArr2 = this.sheetNames;
        String[] strArr3 = new String[strArr2.length + 1];
        strArr3[strArr2.length] = str;
        this.sheetNames = strArr3;
        return strArr3.length - 1;
    }

    public String getSheetName(int i2) {
        return this.sheetNames[i2];
    }

    public SupbookType getType() {
        return this.type;
    }

    private void initInternal() {
        byte[] bArr = new byte[4];
        this.data = bArr;
        IntegerHelper.getTwoBytes(this.numSheets, bArr, 0);
        byte[] bArr2 = this.data;
        bArr2[2] = 1;
        bArr2[3] = 4;
        this.type = INTERNAL;
    }

    public SupbookRecord(int i2, WorkbookSettings workbookSettings) {
        super(Type.SUPBOOK);
        this.numSheets = i2;
        this.type = INTERNAL;
        this.workbookSettings = workbookSettings;
    }

    public SupbookRecord(String str, WorkbookSettings workbookSettings) {
        super(Type.SUPBOOK);
        this.fileName = str;
        this.numSheets = 1;
        this.sheetNames = new String[0];
        this.workbookSettings = workbookSettings;
        this.type = EXTERNAL;
    }

    public SupbookRecord(jxl.read.biff.SupbookRecord supbookRecord, WorkbookSettings workbookSettings) {
        super(Type.SUPBOOK);
        this.workbookSettings = workbookSettings;
        if (supbookRecord.getType() == jxl.read.biff.SupbookRecord.INTERNAL) {
            this.type = INTERNAL;
            this.numSheets = supbookRecord.getNumberOfSheets();
        } else if (supbookRecord.getType() == jxl.read.biff.SupbookRecord.EXTERNAL) {
            this.type = EXTERNAL;
            this.numSheets = supbookRecord.getNumberOfSheets();
            this.fileName = supbookRecord.getFileName();
            this.sheetNames = new String[this.numSheets];
            for (int i2 = 0; i2 < this.numSheets; i2++) {
                this.sheetNames[i2] = supbookRecord.getSheetName(i2);
            }
        }
        if (supbookRecord.getType() == jxl.read.biff.SupbookRecord.ADDIN) {
            logger.warn("Supbook type is addin");
        }
    }
}
