import {Component, Inject, LOCALE_ID, OnInit} from '@angular/core';
import {Crop, Language, Pest, Project, Region, Resource, ResourceType, Sector} from "../../model/resource";
import {FormControl} from "@angular/forms";
import {environment} from "../../../environments/environment";
import {ResourceService} from "../../service/resource.service";
import {ActivatedRoute, Router} from "@angular/router";
import {ConfirmationService, MessageService} from "primeng/api";

@Component({
  selector: 'app-request-resource',
  templateUrl: './request-resource.component.html',
  styleUrls: ['./request-resource.component.css']
})
export class RequestResourceComponent implements OnInit {
  sectors: Sector[] = [];
  regions: Region[] = [];
  resourceTypes: ResourceType[] = [];
  pests:Pest[] = [];
  crops: Crop [] = [];
  resource: Resource = {};
  displayPestModal=false;
  displayCropModal=false;
  newPest:Pest = {};
  newCrop: Crop = {};
  resourceControl = new FormControl('');
  displayTerms = false;
  termChecked = false;

  constructor(private resourceService:ResourceService, private route: ActivatedRoute, private messageService: MessageService, private router: Router, @Inject(LOCALE_ID) public locale: string, private confirmationService: ConfirmationService) { }

  ngOnInit(): void {
    this.resourceService.getAllSectors().subscribe((sectors: Sector[]) => {
      this.sectors = sectors;});
    this.resourceService.getAllRegions().subscribe((regions: Region[]) => {
      this.regions = regions;});
    this.resourceService.getAllPests().subscribe((pests: Pest[]) => {
      this.pests = pests;});
    this.resourceService.getAllCrops().subscribe((crops: Crop[]) => {
      this.crops = crops;});
    this.resourceService.getAllResourceTypes().subscribe((contentTypes: ResourceType[]) => {
      this.resourceTypes = contentTypes;});
  }

  saveResource(){
    this.resource.canEdit=true;
    // @ts-ignore
    this.resource.approved=false
    this.resource.resourceType = this.resourceTypes.filter(r=>r.name=='Request')[0]
    this.resourceService.saveResource(this.resource,false).subscribe((data: Resource) => {
      this.resource = data;
      this.router.navigate(['/home']);
    },error => {
      this.messageService.add({severity: 'error', detail: 'Some error occurred!'})
    });
  }

  public openPestModal(){
    // @ts-ignore
    if (this.resource.pests.filter(e => e.commonName === 'Other').length > 0) {
      this.displayPestModal=true;
    }
  }
  public addPest(){
    // @ts-ignore
    var selectedPests = this.resource.pests.filter(function(el) { return el.commonName != "Other"; });
    this.resource.pests = selectedPests;
    this.resource.pests.push(this.newPest);
    this.pests.push(this.newPest);
   // this.resourceService.addCrop(this.newPest).subscribe()
    this.newPest={};
    this.displayPestModal=false;
  }

  public openCropModal(){
    // @ts-ignore
    if (this.resource.crops.filter(e => e.commonName === 'Other').length > 0) {
      this.displayCropModal=true;
    }
  }

  public addCrop(){
    // @ts-ignore
    var selectedCrops = this.resource.crops.filter(function(el) { return el.commonName != "Other"; });
    this.resource.crops = selectedCrops;
    this.resource.crops.push(this.newCrop);
    this.crops.push(this.newCrop);
    //this.resourceService.addCrop(this.newCrop).subscribe()
    this.newCrop={};
    this.displayCropModal=false;
  }
  showTerms(){
    this.displayTerms = true;
  }
}
