import { fireEvent, render, waitFor } from '@testing-library/react-native';
import {
  DeviceEventEmitter,
  LayoutChangeEvent,
  Platform,
  Text,
  View,
} from 'react-native';
import WebView from 'react-native-webview';
import Logger from '../../core/logging/logger';
import { CSQWebView } from '../../webview/CSQWebView.android';
import {
  registerWebView,
  unregisterWebView,
} from '../../webview/csqWebViewMethods';

jest.mock('../../core/logging/logger', () => ({
  error: jest.fn(),
  warn: jest.fn(),
}));

jest.mock('../../webview/csqWebViewMethods', () => ({
  registerWebView: jest.fn(),
  unregisterWebView: jest.fn(),
}));

jest.mock('react-native-webview', () => ({
  __esModule: true,
  default: 'WebView',
}));

describe('CSQWebView', () => {
  beforeEach(() => {
    Platform.OS = 'android';
    jest.clearAllMocks();
  });

  afterEach(() => {
    jest.restoreAllMocks();
    jest.restoreAllMocks();
  });

  describe('Implementation 2: Child WebView', () => {
    it('should render a child webview with the correct props', () => {
      const mockOnLayout = jest.fn();

      const { getByTestId } = render(
        <CSQWebView>
          <View onLayout={mockOnLayout} testID="child-webview" />
        </CSQWebView>
      );

      const childWebView = getByTestId('child-webview');
      expect(childWebView).toBeTruthy();
    });

    it('should log an error if more than one child is passed', () => {
      render(
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore - Testing with multiple children
        <CSQWebView>
          <View />
          <View />
        </CSQWebView>
      );

      expect(Logger.error).toHaveBeenCalledWith(
        'CSQWebView component expects exactly 1 child, but received multiple. Ensure that only a single child element is passed'
      );
    });

    it('should inject webview on layout', () => {
      const mockEvent = {
        nativeEvent: { target: 123 },
      } as unknown as LayoutChangeEvent;

      const { getByTestId } = render(
        <CSQWebView>
          <View testID="child-webview" />
        </CSQWebView>
      );

      const childWebView = getByTestId('child-webview');
      fireEvent(childWebView, 'layout', mockEvent);

      expect(registerWebView).toHaveBeenCalledWith(123);
    });

    it('should call onLayout prop from children', () => {
      const mockOnLayout = jest.fn();

      const { getByTestId } = render(
        <CSQWebView>
          <View onLayout={mockOnLayout} testID="child-webview" />
        </CSQWebView>
      );

      const childWebView = getByTestId('child-webview');
      fireEvent(childWebView, 'layout', {
        nativeEvent: { layout: { width: 100, height: 100 } },
      });

      expect(mockOnLayout).toHaveBeenCalled();
    });

    it('should set applicationNameForUserAgent to "CS_WebView" when no original applicationNameForUserAgent is provided', () => {
      const { getByTestId } = render(
        <CSQWebView>
          <WebView testID="webview" />
        </CSQWebView>
      );

      const webView = getByTestId('webview');
      expect(webView.props.applicationNameForUserAgent).toContain('CS_WebView');
    });

    it('should append "CS_WebView" to existing userAgent', () => {
      const originalUserAgent = 'Mozilla/5.0 (Linux; Android 10; SM-G975F)';

      const { getByTestId } = render(
        <CSQWebView>
          <WebView applicationNameForUserAgent={originalUserAgent} testID="webview" />
        </CSQWebView>
      );

      const webView = getByTestId('webview');
      expect(webView.props.applicationNameForUserAgent).toContain(`${originalUserAgent} CS_WebView`);
    });

    it('should work with empty string applicationNameForUserAgent', () => {
      const { getByTestId } = render(
        <CSQWebView>
          <WebView applicationNameForUserAgent="" testID="webview" />
        </CSQWebView>
      );

      const webView = getByTestId('webview');
      expect(webView.props.applicationNameForUserAgent).toContain('CS_WebView');
    });

    it('should work with undefined applicationNameForUserAgent', () => {
      const { getByTestId } = render(
        <CSQWebView>
          <WebView applicationNameForUserAgent={undefined} testID="webview" />
        </CSQWebView>
      );

      const webView = getByTestId('webview');
      expect(webView.props.applicationNameForUserAgent).toContain('CS_WebView');
    });
  });

  it('should remove onCSQWebViewInjectedListener on unmount', () => {
    const mockRenderWebView = jest.fn((onLayout, webViewUrl) => (
      <View onLayout={onLayout} testID="webview">
        <Text>{webViewUrl}</Text>
      </View>
    ));

    const removeListener = jest.fn();
    jest.spyOn(DeviceEventEmitter, 'addListener').mockImplementation(() => ({
      remove: removeListener,
    }));

    const { unmount } = render(
      <CSQWebView renderWebView={mockRenderWebView} url="https://example.com" />
    );

    unmount();
    expect(removeListener).toHaveBeenCalled();
  });

  it('should call unregisterWebView on unmount', async () => {
    const { getByTestId, unmount } = render(
      <CSQWebView>
        <View testID="child-webview" />
      </CSQWebView>
    );

    const childWebView = getByTestId('child-webview');
    fireEvent(childWebView, 'layout', {
      nativeEvent: { layout: { width: 100, height: 100 }, target: 123 },
    });

    await waitFor(() => expect(registerWebView).toHaveBeenCalledWith(123));
    DeviceEventEmitter.emit('onCSQWebViewInjected');
    unmount();
    await waitFor(() => expect(unregisterWebView).toHaveBeenCalled());
  });
});
