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()));

      /*  byte[] b = Util.hexStringToByteArray("0A120900000000000000021100000000000000001212094743545702000002116739AB04000000001A1C575443472E5574696C4E6F74696669636174696F6E4D65737361676522130A0C6D6573736167655F74797065120318E00122120A0C6D6573736167655F73697A65120218002A1209000000000000000111000000000000000032120900000000000000011193710E1A000000003A004A0A0889FF5C1092E5A1B90552150A050D93710E1A120C0D6739AB0415474354571802");

        header = Protocol.Header.newBuilder()
                .setServiceId(4)
                .setMethodId(1)
                .setToken(conn.nextToken())
                .setObjectId(0)
                .setSize(b.length)
                .setStatus(0)
                .build();
        conn.send(new BattleNetPacket(header, b));*/

        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());
            }
        }
    }
}
