/*
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 React from "react";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MatrixClient, MatrixEvent, RelationType } from "matrix-js-sdk/src/matrix";
import { Relations } from "matrix-js-sdk/src/models/relations";
import { mocked } from "jest-mock";

import {
    VoiceBroadcastBody,
    VoiceBroadcastInfoEventType,
    VoiceBroadcastInfoState,
    VoiceBroadcastRecordingBody,
} from "../../../src/voice-broadcast";
import { mkEvent, stubClient } from "../../test-utils";
import { IBodyProps } from "../../../src/components/views/messages/IBodyProps";

jest.mock("../../../src/voice-broadcast/components/molecules/VoiceBroadcastRecordingBody", () => ({
    VoiceBroadcastRecordingBody: jest.fn(),
}));

describe("VoiceBroadcastBody", () => {
    const roomId = "!room:example.com";
    const recordingTestid = "voice-recording";
    let client: MatrixClient;
    let getRelationsForEvent: (eventId: string, relationType: string, eventType: string) => Relations;
    let event: MatrixEvent;
    let relatedEvent: MatrixEvent;
    let recordingElement: HTMLElement;

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

    const renderVoiceBroadcast = async () => {
        const props: IBodyProps = {
            getRelationsForEvent,
            mxEvent: event,
        } as unknown as IBodyProps;
        const result = render(<VoiceBroadcastBody {...props} />);
        recordingElement = await result.findByTestId(recordingTestid);
    };

    const itShouldRenderALiveVoiceBroadcast = () => {
        it("should render a live voice broadcast", () => {
            expect(VoiceBroadcastRecordingBody).toHaveBeenCalledWith(
                {
                    onClick: expect.any(Function),
                    live: true,
                    member: event.sender,
                    userId: client.getUserId(),
                    title: "@userId:matrix.org • My room",
                },
                {},
            );
        });
    };

    const itShouldRenderANonLiveVoiceBroadcast = () => {
        it("should render a non-live voice broadcast", () => {
            expect(VoiceBroadcastRecordingBody).toHaveBeenCalledWith(
                {
                    onClick: expect.any(Function),
                    live: false,
                    member: event.sender,
                    userId: client.getUserId(),
                    title: "@userId:matrix.org • My room",
                },
                {},
            );
        });
    };

    beforeEach(() => {
        mocked(VoiceBroadcastRecordingBody).mockImplementation(
            ({
                live,
                member: _member,
                onClick,
                title,
                userId: _userId,
            }) => {
                return (
                    <div
                        data-testid={recordingTestid}
                        onClick={onClick}
                    >
                        { title }
                        { live && "Live" }
                    </div>
                );
            },
        );
        client = stubClient();
        event = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started);
    });

    describe("when getRelationsForEvent is undefined", () => {
        beforeEach(async () => {
            await renderVoiceBroadcast();
        });

        itShouldRenderALiveVoiceBroadcast();

        describe("and the Voice Broadcast tile has been clicked", () => {
            beforeEach(async () => {
                await userEvent.click(recordingElement);
            });

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

    describe("when getRelationsForEvent returns null", () => {
        beforeEach(async () => {
            getRelationsForEvent = jest.fn().mockReturnValue(null);
            await renderVoiceBroadcast();
        });

        itShouldRenderALiveVoiceBroadcast();
    });

    describe("when getRelationsForEvent returns a stopped Voice Broadcast info", () => {
        beforeEach(async () => {
            relatedEvent = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped);
            getRelationsForEvent = jest.fn().mockReturnValue({
                getRelations: jest.fn().mockReturnValue([
                    relatedEvent,
                ]),
            });
            await renderVoiceBroadcast();
        });

        itShouldRenderANonLiveVoiceBroadcast();

        describe("and the Voice Broadcast tile has been clicked", () => {
            beforeEach(async () => {
                await userEvent.click(recordingElement);
            });

            it("should not emit a voice broadcast stop state event", () => {
                expect(mocked(client.sendStateEvent)).not.toHaveBeenCalled();
            });
        });
    });
});
