package com.alterdekim.hearthhack.component.processor.client.request.generic;

import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

import static com.alterdekim.hearthhack.util.GameUtilities.generateNotification;
import static com.alterdekim.hearthhack.util.GetAccountInfoRequest.FEATURES;

@Slf4j
public class AvailableFeatures extends GenericParser {
    private Executor executor;

    private void executeFeatures(TcpConnection conn) throws Exception {
        Protocol.GuardianVars guardianVars = Protocol.GuardianVars.newBuilder()
                .setShowUserUi(1)
                .setClientOptionsUpdateIntervalSeconds(300)
                .build();

        Protocol.Notification n = generateNotification(264, guardianVars.toByteString(), guardianVars.getSerializedSize());

        Protocol.Header header = Protocol.Header.newBuilder()
                .setServiceId(4)
                .setMethodId(1)
                .setToken(conn.nextToken())
                .setStatus(0)
                .setObjectId(0)
                .setSize(n.getSerializedSize())
                .build();

        conn.send(new BattleNetPacket(header, n.toByteArray()));

        List<Protocol.BoosterInfo> binfo = conn.getUserService().getBoostersByUserId(conn.getUserId()).stream().map(b -> Protocol.BoosterInfo.newBuilder()
                .setCount(b.getCount())
                .setType(b.getType())
                .build()
        ).collect(Collectors.toList());

        Protocol.BoosterList boosterList = Protocol.BoosterList.newBuilder()
                .addAllList(binfo)
                .build();

        n = generateNotification(224, boosterList.toByteString(), boosterList.getSerializedSize());

        header = Protocol.Header.newBuilder()
                .setServiceId(4)
                .setMethodId(1)
                .setToken(conn.nextToken())
                .setObjectId(0)
                .setSize(n.getSerializedSize())
                .setStatus(0)
                .build();

        conn.send(new BattleNetPacket(header, n.toByteArray()));
    }

    @Override
    public void parseGenericRequest(int token, TcpConnection conn) throws Exception {
        if( this.executor != null ) this.executor.setRunning(false);
        this.executor = new Executor(conn);
        this.executor.start();
    }

    @Override
    public int getId() {
        return FEATURES.getValue();
    }

    @RequiredArgsConstructor
    private class Executor extends Thread {

        private final TcpConnection conn;
        @Setter
        private boolean isRunning = true;

        @Override
        public void run() {
            try {
                while(isRunning) {
                    Thread.sleep(2500);
                    executeFeatures(conn);
                }
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }
}
