import { HttpClient } from '@angular/common/http';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatListModule } from '@angular/material/list';
import { By } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { of } from 'rxjs';

import { TeamDetailsComponent } from './team-details.component';

describe('TeamDetailsComponent', () => {
  let component: TeamDetailsComponent;
  let fixture: ComponentFixture<TeamDetailsComponent>;
  const paramMap = jasmine.createSpyObj('ParamMap', ['get'])
  const fakeActivatedRoute = {
    snapshot: { paramMap: paramMap }
  }

  paramMap.get.and.returnValue('9999');

  const mockhttp = jasmine.createSpyObj('HttpClient', ['get']);
  mockhttp.get.and.returnValue(of({
    alias: "9999",
    balance: 48.09,
    name: "Development Team",
    outstanding: -582.00,
    players: [{id: 1, firstName: 'Filiberto', lastName: 'Spencer', balance: -32.97}, {id: 2, firstName: 'Adam', lastName: 'West', balance: -50}],
    settings: []
  }))


  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ TeamDetailsComponent ],
      providers: [ 
        {provide: ActivatedRoute, useValue: fakeActivatedRoute},
        {provide: HttpClient, useValue: mockhttp},
       ],
      imports: [ MatListModule ]
    })
    .compileComponents();

    fixture = TestBed.createComponent(TeamDetailsComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should extract the id from the url', () => {
    expect(component.id).toEqual("9999");
  });

  it('should load the teams data', () => {
    expect(component.team).not.toBeNull();
  });

  it('should display name and alias', () => {
    const nameElement = fixture.debugElement.query(By.css('#teamName'));
    const text = (nameElement.nativeElement as HTMLElement).textContent;
    expect(text).toEqual(`${component.team.name} (${component.team.alias})`)
    
    // const htmlList = (list.nativeElement as HTMLElement).children;
    // expect(htmlList).not.toBeNull();
    // expect(htmlList.length).toBe(4)
  });

  it('should display a list with players', () => {
    const element = fixture.debugElement.query(By.css('#playerlist'));
    const ch = (element.nativeElement as HTMLElement).children;
    expect(ch.length).toBe(component.team.players.length);
    for (let index = 0; index < component.team.players.length; index++) {
      const playerName = ch[index].textContent?.trim();
      const expectedName = `${component.team.players[index].firstName} ${component.team.players[index].lastName}`;
      expect(playerName).toEqual(expectedName)
    }
  });

  it('button should be disabled', () => {
    const element = fixture.debugElement.query(By.css('#buttonNewTransaction'));
    expect(element.nativeElement.disabled).toBeTrue();
  });

  it('should enable the button when selecting a player', () => {
    const element = fixture.debugElement.query(By.css('.mdc-checkbox'));
    element.nativeElement.click();
    fixture.detectChanges();
    const buttonelement = fixture.debugElement.query(By.css('#buttonNewTransaction'));
    expect(buttonelement.nativeElement.disabled).toBeFalse();
    
  })
});