import {getValidPackages, isValidPackage, PackageService} from './package.service';
import {MockStore, provideMockStore} from '@ngrx/store/testing';
import {TestBed} from '@angular/core/testing';
import * as appCommonReducer from '../reducers/app-common.reducer';
import {reducer, State} from '../reducers/app-common.reducer';
import * as appCommonActions from '../actions/app-common.actions';
import {IItem} from '../models/item';
import {IPackage} from '../models/package';

describe('PackageService', () => {
  const initialState = {userPackages: {}} as State;
  let serviceUnderTest: PackageService;
  let items;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        provideMockStore({
          initialState,
          selectors: [
            {
              selector: appCommonReducer.SelectGetValidUserPackages,
              value: {
                'vnd.farmmaps.package.zoning': {
                  id: 'vnd.farmmaps.package.zoning',
                  name: 'zoning package',
                  dataDate: '2022-01-25T00:00:00.000000Z'
                }
              }
            }
          ]
        }),
      ]
    });

    const store = TestBed.inject(MockStore);
    serviceUnderTest = new PackageService(store, null, null);

    items = [
      {
        code: '123132',
        dataDate: '2021-01-25T00:00:00Z',
        dataEndDate: '2021-05-25T00:00:00Z',
        data: {
          id: 'vnd.farmmaps.package.zoning'
        }
      } as IItem,
      {
        code: '1231325',
        dataDate: '2021-01-25T00:00:00Z',
        dataEndDate: null,
        data: {
          id: 'vnd.farmmaps.package.zoning'
        }
      } as IItem,
      {
        code: '1231325',
        dataDate: '2022-01-25T00:00:00Z',
        dataEndDate: '2022-05-25T00:00:00Z',
        data: {
          id: 'vnd.farmmaps.package.tipstar'
        }
      } as IItem,
      {
        code: '1231325',
        dataDate: '2022-01-25T00:00:00Z',
        dataEndDate: null,
        data: {
          id: 'vnd.farmmaps.package.weather'
        }
      } as IItem
    ];
  });

  it('Does not have a tipstar package', () => {
    const hasPackage = serviceUnderTest.hasPackage('vnd.farmmaps.package.tipstar');
    expect(hasPackage).toBe(false);
  });

  it('Does have a zoning package', () => {
    const hasPackage = serviceUnderTest.hasPackage('vnd.farmmaps.package.zoning');
    expect(hasPackage).toBe(true);
  });

  it('reducer.INITUSERPACKAGESSUCCESS alter state correctly', () => {
    const action = new appCommonActions.InitUserPackagesSuccess(items);

    expect(Object.keys(initialState.userPackages).length).toBe(0);
    const state = reducer(initialState, action);
    expect(Object.keys(state.userPackages).length).toBe(3);
    expect(state.userPackages['vnd.farmmaps.package.zoning'].length).toBe(2);
    expect(state.userPackages['vnd.farmmaps.package.tipstar'].length).toBe(1);
    expect(state.userPackages['vnd.farmmaps.package.weather'].length).toBe(1);
  });

  it('to validatePackage', () => {
    const action = new appCommonActions.InitUserPackagesSuccess(items);
    const state = reducer(initialState, action);

    expect(isValidPackage(null)).toBe(false);
    expect(isValidPackage(state.userPackages['vnd.farmmaps.package.tipstar'][0])).toBe(true);
    expect(isValidPackage(state.userPackages['vnd.farmmaps.package.zoning'][0])).toBe(false);
    expect(isValidPackage(state.userPackages['vnd.farmmaps.package.zoning'][1])).toBe(true);
    expect(isValidPackage(state.userPackages['vnd.farmmaps.package.weather'][0])).toBe(true);
  });

  it('to getValidPackages', () => {
    const action = new appCommonActions.InitUserPackagesSuccess(items);
    const state = reducer(initialState, action);

    const validPackages = getValidPackages(state.userPackages);

    expect(Object.keys(validPackages).length).toBe(3);

    // today is 27-01-2022, so the below package is not valid
    expect(validPackages['vnd.farmmaps.package.zoning']).not.toEqual({
      id: 'vnd.farmmaps.package.zoning',
      dataDate: '2021-01-25T00:00:00Z',
      dataEndDate: '2021-05-25T00:00:00Z'
    } as unknown as IPackage);
  });
});
