import { Component, OnInit } from '@angular/core';
import { BaseComponent } from '../base/base.component';
import { CvService } from '../services/cv.service';
import { IProjects } from './projects.model';
import { IProject } from '../models/project.model';
import { HttpClient } from '@angular/common/http';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-projects',
  templateUrl: './projects.component.html',
  styleUrl: './projects.component.scss'
})
export class ProjectsComponent extends BaseComponent<IProjects> implements OnInit {
filter: string = 'All';
projects!: IProject[];
subscription: Subscription = <Subscription>{};
categoryClicked: boolean = false;
  constructor(svc: CvService, public http: HttpClient){
    super(svc);
  }

  ngOnInit(): void {
    this.getProjects();
  }

  getProjects(){
    this.svc.getProjects(this.candidateId).subscribe((response: IProjects) => {
      this.svc.projects = this.svc.projects ?? response;
      this.projects = response.projects;
      this.assignData(response);
    });
  }

  filterProjects(category: string) {
    this.filter = category;
    this.projects = this.filter === 'All'
      ? this.model.projects
      : this.model.projects.filter(
        (project: IProject) => {
          return project.categories.includes(category)
        });
  }
}
