package net.northking.cctp.upperComputer.driver.agent.command;

import net.northking.cctp.upperComputer.driver.agent.command.data.PackageInfo;
import net.northking.cctp.upperComputer.driver.agent.command.protocol.AbstractProtocolCommand;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;

public class PackageListCommand extends AbstractProtocolCommand<ArrayList<PackageInfo>> {

    private static PackageListCommand instance;

    public static PackageListCommand getInstance() {
        if (instance == null) {
            instance = new PackageListCommand();
        }
        return instance;
    }

    @Override
    public int getCommandId() {
        return 39;
    }

    @Override
    public void doSend(DataOutput output) throws IOException {

    }

    @Override
    public ArrayList<PackageInfo> buildReceive(DataInput input) throws IOException {
        ArrayList<PackageInfo> list = new ArrayList<>();
        int count = input.readInt();
        for (int i = 0; i < count; i++) {
            PackageInfo packageInfo = new PackageInfo();
            packageInfo.setPackageName(readText(input));
            packageInfo.setFlags(input.readInt());
            packageInfo.setClassName(readNullable(input, this::readText));
            packageInfo.setProcessName(readText(input));
            packageInfo.setCompatibleWidthLimitDp(input.readInt());
            packageInfo.setManageSpaceActivityName(readNullable(input, this::readText));
            packageInfo.setBackupAgentName(readNullable(input, this::readText));
            packageInfo.setSourceDir(readText(input));
            packageInfo.setDataDir(readText(input));
            packageInfo.setUid(input.readInt());
            packageInfo.setLabel(readText(input));

            String[] splitNames = new String[input.readInt()];
            for (int j = 0; j < splitNames.length; j++) {
                splitNames[j] = readText(input);
            }
            packageInfo.setSplitNames(splitNames);
            packageInfo.setVersionCode(input.readLong());
            packageInfo.setVersionName(readText(input));
            packageInfo.setBaseRevisionCode(input.readInt());

            int[] splitRevisionCodes = new int[input.readInt()];
            for (int j = 0; j < splitRevisionCodes.length; j++) {
                splitRevisionCodes[j] = input.readInt();
            }
            packageInfo.setSplitRevisionCodes(splitRevisionCodes);
            packageInfo.setSharedUserId(readNullable(input, this::readText));
            packageInfo.setSharedUserLabel(input.readInt());
            packageInfo.setFirstInstallTime(input.readLong());
            packageInfo.setLastUpdateTime(input.readLong());

            int[] gids = new int[input.readInt()];
            for (int j = 0; j < gids.length; j++) {
                gids[j] = input.readInt();
            }
            packageInfo.setGids(gids);

            String[] requestedPermissions = new String[input.readInt()];
            for (int j = 0; j < requestedPermissions.length; j++) {
                requestedPermissions[j] = readText(input);
            }
            packageInfo.setRequestedPermissions(requestedPermissions);

            int[] requestedPermissionsFlags = new int[input.readInt()];
            for (int j = 0; j < requestedPermissionsFlags.length; j++) {
                requestedPermissionsFlags[j] = input.readInt();
            }
            packageInfo.setRequestedPermissionFlags(requestedPermissionsFlags);

//            var signatures = new String[input.readInt()];
//            for (int j = 0; j < signatures.length; j++) {
//                signatures[j] = readText(input);
//            }
//            packageInfo.setSignatures(signatures);
//
//            var apkContentsSigners = new String[input.readInt()];
//            for (int j = 0; j < apkContentsSigners.length; j++) {
//                apkContentsSigners[j] = readText(input);
//            }
//            packageInfo.setApkContentsSigners(apkContentsSigners);
//
//            var signingCertificateHistory = new String[input.readInt()];
//            for (int j = 0; j < signingCertificateHistory.length; j++) {
//                signingCertificateHistory[j] = readText(input);
//            }
//            packageInfo.setSigningCertificateHistory(signingCertificateHistory);
//            packageInfo.setHasMultipleSigners(input.readBoolean());
//            packageInfo.setHasPastSigningCertificates(input.readBoolean());
            packageInfo.setApex(input.readBoolean());
            list.add(packageInfo);
        }
        return list;
    }
}
