import {Widget} from '@bthlabs/homehub-components';
import {ServiceState} from '@bthlabs/homehub-core';
import {shallow} from 'enzyme';
import React from 'react';

import * as UptimeWidgetView from 'src/widgets/UptimeWidgetView';

import {FakeService} from 'tests/__fixtures__/services';

describe('src/widgets/UptimeWidgetView', () => {
  describe('UptimeWidgetView', () => {
    let fakeAppearance = null;
    let fakeService = null;
    let fakeServiceState = null;

    beforeEach(() => {
      fakeAppearance = {color: 'red'};
      fakeService = new FakeService({
        instance: 'testing',
        layout: {x: 0, y: 0, h: 1, w: 1},
      });
      fakeServiceState = new ServiceState({
        data: 123,
      });
    });

    it('defines the widget attributes', () => {
      // Then
      expect(UptimeWidgetView.UptimeWidgetView.defaultLayout).toEqual({
        h: jasmine.any(Number),
        w: jasmine.any(Number),
      });
      expect(UptimeWidgetView.UptimeWidgetView.icon).toBe(null);
      expect(UptimeWidgetView.UptimeWidgetView.layoutConstraints).toEqual({
        minH: jasmine.any(Number),
        minW: jasmine.any(Number),
      });
      expect(UptimeWidgetView.UptimeWidgetView.settingsView).toEqual(
        UptimeWidgetView.TimeWidgetSettingsView
      );
      expect(UptimeWidgetView.UptimeWidgetView.title).toEqual('Uptime');
    });

    it('renders empty when service state is null', () => {
      // Given
      fakeServiceState = null;
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // Then
      expect(component.isEmptyRender()).toBe(true);
    });

    it('configures and renders the Widget', () => {
      // Given
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const widget = component.find(Widget).at(0);

      // Then
      expect(widget.exists()).toBe(true);
      expect(widget.prop('appearance')).toEqual(fakeAppearance);
      expect(widget.prop('instance')).toEqual(fakeService.instance);
      expect(widget.prop('kind')).toEqual(fakeService.kind);
      expect(widget.prop('layout')).toEqual(fakeService.layout);
    });

    it('rendes the formatted uptime interval for 1 day', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 86401});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('1 day');
    });

    it('rendes the formatted uptime interval for 2 days', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 86401 * 2});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('2 days');
    });

    it('rendes the formatted uptime interval for 1 hour', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 3601});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('1 hour');
    });

    it('rendes the formatted uptime interval for 2 hours', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 3601 * 2});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('2 hours');
    });

    it('rendes the formatted uptime interval for 1 minute', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 61});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('1 minute');
    });

    it('rendes the formatted uptime interval for 2 minutes', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 61 * 2});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('2 minutes');
    });

    it('rendes the formatted uptime interval for 1 second', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 1});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('1 second');
    });

    it('rendes the formatted uptime interval for 2 seconds', () => {
      // Given
      fakeServiceState = fakeServiceState.update({data: 2});
      const component = shallow(
        <UptimeWidgetView.UptimeWidgetView
          appearance={fakeAppearance}
          service={fakeService}
          serviceState={fakeServiceState}
        />
      );

      // When
      const h4 = component.find('h4').at(0);

      // Then
      expect(h4.text()).toEqual('2 seconds');
    });


  });
});
