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

import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.ClientRequestBody;

import java.util.stream.IntStream;

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

public class GetAchieves extends ClientRequestParser {

    private final Protocol.Date d = Protocol.Date.newBuilder()
            .setYear(2016)
            .setMonth(4)
            .setDay(24)
            .setHours(9)
            .setMin(36)
            .setSec(21)
            .build();

    @Override
    public void parse(BattleNetPacket packet, ClientRequestBody body, TcpConnection conn) throws Exception {
        Protocol.Achieves.Builder achieves = Protocol.Achieves.newBuilder();

        IntStream.range(1, 100).filter(i -> i != 79).forEach(i -> achieves.addList(Protocol.Achieve.newBuilder()
                .setId(i)
                .setProgress(1)
                .setAckProgress(1)
                .setCompletionCount(1)
                .setStartedCount(1)
                .setDateGiven(d)
                .setDateCompleted(d)));

        achieves.addList(Protocol.Achieve.newBuilder()
                .setId(79)
                .setProgress(11)
                .setActive(false)
                .setAckProgress(10)
                .setDoNotAck(true)
                .setCompletionCount(1)
                .setStartedCount(1)
                .setDateGiven(d)
                .setDateCompleted(d));

        Protocol.Achieves r = achieves.build();

        Protocol.Notification n = generateNotification(252, r.toByteString(), r.getSerializedSize());

        Protocol.Header 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 int getId() {
        return 253; // TODO: make enum with all request/response codes
    }
}
