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


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;
import java.util.List;

public class RunningAppProcessInfoCommand extends AbstractProtocolCommand<List<RunningAppProcessInfo>> {

    private static RunningAppProcessInfoCommand instance;

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

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

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

    }

    @Override
    public List<RunningAppProcessInfo> buildReceive(DataInput input) throws IOException {
        int count = input.readInt();
        List list = new ArrayList<RunningAppProcessInfo>();
        for (int i = 0; i < count; i++) {
            RunningAppProcessInfo info = new RunningAppProcessInfo();
            info.setPid(input.readInt());
            info.setUid(input.readInt());
            info.setProcessName(readText(input));
            info.setLru(input.readInt());
            info.setImportance(input.readInt());
            info.setImportanceReasonComponent(readNullable(input, this::readText));
            info.setImportanceReasonPid(input.readInt());
            info.setLastTrimLevel(input.readInt());
            int pkgListSize = input.readInt();
            List pkgList = new ArrayList<String>();
            for (int j = 0; j < pkgListSize; j++) {
                pkgList.add(readText(input));
            }
            info.setPkgList(pkgList);
            list.add(info);
        }
        return list;
    }
}
