/*
Copyright 2022 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { mocked } from "jest-mock";
import { EventTimelineSet, EventType, MatrixClient, MatrixEvent, RelationType, Room } from "matrix-js-sdk/src/matrix";
import { Relations } from "matrix-js-sdk/src/models/relations";

import {
    VoiceBroadcastInfoEventContent,
    VoiceBroadcastInfoEventType,
    VoiceBroadcastInfoState,
    VoiceBroadcastRecording,
    VoiceBroadcastRecordingEvent,
} from "../../../src/voice-broadcast";
import { mkEvent, mkStubRoom, stubClient } from "../../test-utils";

describe("VoiceBroadcastRecording", () => {
    const roomId = "!room:example.com";
    let room: Room;
    let client: MatrixClient;
    let infoEvent: MatrixEvent;
    let voiceBroadcastRecording: VoiceBroadcastRecording;
    let onStateChanged: (state: VoiceBroadcastInfoState) => void;

    const mkVoiceBroadcastInfoEvent = (content: VoiceBroadcastInfoEventContent) => {
        return mkEvent({
            event: true,
            type: VoiceBroadcastInfoEventType,
            user: client.getUserId(),
            room: roomId,
            content,
        });
    };

    const setUpVoiceBroadcastRecording = () => {
        voiceBroadcastRecording = new VoiceBroadcastRecording(infoEvent, client);
        voiceBroadcastRecording.on(VoiceBroadcastRecordingEvent.StateChanged, onStateChanged);
    };

    beforeEach(() => {
        client = stubClient();
        room = mkStubRoom(roomId, "Test Room", client);
        mocked(client.getRoom).mockImplementation((getRoomId: string) => {
            if (getRoomId === roomId) {
                return room;
            }
        });
        onStateChanged = jest.fn();
    });

    afterEach(() => {
        voiceBroadcastRecording.off(VoiceBroadcastRecordingEvent.StateChanged, onStateChanged);
    });

    describe("when created for a Voice Broadcast Info without relations", () => {
        beforeEach(() => {
            infoEvent = mkVoiceBroadcastInfoEvent({
                state: VoiceBroadcastInfoState.Started,
            });
            setUpVoiceBroadcastRecording();
        });

        it("should be in Started state", () => {
            expect(voiceBroadcastRecording.state).toBe(VoiceBroadcastInfoState.Started);
        });

        describe("and calling stop()", () => {
            beforeEach(() => {
                voiceBroadcastRecording.stop();
            });

            it("should send a stopped Voice Broadcast Info event", () => {
                expect(mocked(client.sendStateEvent)).toHaveBeenCalledWith(
                    roomId,
                    VoiceBroadcastInfoEventType,
                    {
                        state: VoiceBroadcastInfoState.Stopped,
                        ["m.relates_to"]: {
                            rel_type: RelationType.Reference,
                            event_id: infoEvent.getId(),
                        },
                    },
                    client.getUserId(),
                );
            });

            it("should be in state stopped", () => {
                expect(voiceBroadcastRecording.state).toBe(VoiceBroadcastInfoState.Stopped);
            });

            it("should emit a stopped state changed event", () => {
                expect(onStateChanged).toHaveBeenCalledWith(VoiceBroadcastInfoState.Stopped);
            });
        });
    });

    describe("when created for a Voice Broadcast Info with a Stopped relation", () => {
        beforeEach(() => {
            infoEvent = mkVoiceBroadcastInfoEvent({
                state: VoiceBroadcastInfoState.Started,
                chunk_length: 300,
            });

            const relationsContainer = {
                getRelations: jest.fn(),
            } as unknown as Relations;
            mocked(relationsContainer.getRelations).mockReturnValue([
                mkVoiceBroadcastInfoEvent({
                    state: VoiceBroadcastInfoState.Stopped,
                    ["m.relates_to"]: {
                        rel_type: RelationType.Reference,
                        event_id: infoEvent.getId(),
                    },
                }),
            ]);

            const timelineSet = {
                relations: {
                    getChildEventsForEvent: jest.fn().mockImplementation(
                        (
                            eventId: string,
                            relationType: RelationType | string,
                            eventType: EventType | string,
                        ) => {
                            if (
                                eventId === infoEvent.getId()
                                && relationType === RelationType.Reference
                                && eventType === VoiceBroadcastInfoEventType
                            ) {
                                return relationsContainer;
                            }
                        },
                    ),
                },
            } as unknown as EventTimelineSet;
            mocked(room.getUnfilteredTimelineSet).mockReturnValue(timelineSet);

            setUpVoiceBroadcastRecording();
        });

        it("should be in Stopped state", () => {
            expect(voiceBroadcastRecording.state).toBe(VoiceBroadcastInfoState.Stopped);
        });
    });
});