import { TestBed } from '@angular/core/testing';

import { LoginService } from './login.service';
import {LocalStorageService} from 'angular-2-local-storage';
import {ApiService} from './api.service';
import {Router} from '@angular/router';

// TODO

let loginService: LoginService;
let apiServiceSpy: jasmine.SpyObj<ApiService>;
let localStorageServiceSpy: jasmine.SpyObj<LocalStorageService>;
let routerServiceSpy: jasmine.SpyObj<Router>;

describe('LoginService', () => {
  beforeEach(() => {
    const apiSpy = jasmine.createSpyObj('ApiService', ['get']);
    const localStorageSpy = jasmine.createSpyObj('LocalStorageService', ['set', 'remove']);
    const routerSpy = jasmine.createSpyObj('Router', ['navigate']);

    TestBed.configureTestingModule({
      providers: [
        LoginService,
        {provide: ApiService, useValue: apiSpy},
        {provide: LocalStorageService, useValue: localStorageSpy},
        {provide: Router, useValue: routerSpy}
      ]
    });

    loginService = TestBed.inject(LoginService);
    apiServiceSpy = TestBed.inject(ApiService) as jasmine.SpyObj<ApiService>;
    localStorageServiceSpy = TestBed.inject(LocalStorageService) as jasmine.SpyObj<LocalStorageService>;
    routerServiceSpy = TestBed.inject(Router) as jasmine.SpyObj<Router>;
  });

  it('should be created', () => {
    expect(loginService).toBeTruthy();
  });

  // describe('login', () => {
  //   it('should store the basic auth', () => {
  //     localStorageServiceSpy.set.and.returnValue(true);
  //     apiServiceSpy.get.and.callFake(() => {});
  //     loginService.login('username', 'password');
  //     expect(localStorageServiceSpy.set).toHaveBeenCalledWith('basicAuth', 'dXNlcm5hbWU6cGFzc3dvcmQ=');
  //   });
  //
  //   it('should remove the basic auth if login fails', () => {
  //     localStorageServiceSpy.set.and.returnValue(true);
  //     localStorageServiceSpy.remove.and.returnValue(true);
  //     apiServiceSpy.get.and.callFake((a, b) => {b(undefined, 'error'); });
  //     loginService.login('username', 'password');
  //     expect(localStorageServiceSpy.remove.calls.count()).toBe(1);
  //     expect(localStorageServiceSpy.remove).toHaveBeenCalledWith('basicAuth');
  //   });
  //
  //   it('should resolve true when login succeeds', async () => {
  //     localStorageServiceSpy.set.and.returnValue(true);
  //     apiServiceSpy.get.and.callFake((a, b) => {b({status: 'Authorization successful', method: 'basic'} as any, undefined); });
  //     expect(await loginService.login('username', 'password')).toBeTruthy();
  //   });
  //
  //   it('should resolve false when a wrong result comes in', async () => {
  //     localStorageServiceSpy.set.and.returnValue(true);
  //     apiServiceSpy.get.and.callFake((a, b) => {b({status: 'xxx', method: 'basic'} as any, undefined); });
  //     expect(await loginService.login('username', 'password')).toBeFalsy();
  //   });
  //
  //   it('should resolve false on an error', async () => {
  //     localStorageServiceSpy.set.and.returnValue(true);
  //     apiServiceSpy.get.and.callFake((a, b) => {b(undefined, 'error'); });
  //     expect(await loginService.login('username', 'password')).toBeFalsy();
  //   });
  // });
  //
  // describe('canActivate', () => {
  //   it('should return false at first', done => {
  //     apiServiceSpy.get.and.callFake((a, b) => {b(undefined, 'error'); });
  //     loginService.canActivate(null, null).subscribe(res => {
  //       expect(res).toBeFalsy();
  //       done();
  //     });
  //   });
  //
  //   it('returns true if login was successful', async done => {
  //     localStorageServiceSpy.set.and.returnValue(true);
  //     apiServiceSpy.get.and.callFake((a, b) => {b({status: 'Authorization successful', method: 'basic'} as any, undefined); });
  //     await loginService.login('username', 'password');
  //     loginService.canActivate(null, null).subscribe(res => {
  //       expect(res).toBeTruthy();
  //       done();
  //     });
  //   });
  // });
});
