import {Component} from '@angular/core';
import {Material} from "../../../../shared/model/material.model";
import {MaterialService} from "../../../../material/service/material.service";
import {ActivatedRoute, Router} from "@angular/router";
import {SubscribingComponent} from "../../../../shared/components/subscribing.component";
import {MixService} from "../../../services/mix.service";

@Component({
  selector: 'cre-mix-add',
  templateUrl: './mix-add.component.html',
  styleUrls: ['./mix-add.component.sass']
})
export class MixAddComponent extends SubscribingComponent {
  recipeId: number | null
  materials: Material[] | null

  constructor(
    private materialService: MaterialService,
    private mixService: MixService,
    router: Router,
    activatedRoute: ActivatedRoute
  ) {
    super(activatedRoute, router)
  }

  ngOnInit(): void {
    super.ngOnInit()

    this.recipeId = this.urlUtils.parseIntUrlParam('recipeId')

    this.subscribe(
      this.materialService.getAllForMixCreation(this.recipeId),
      {next: m => this.materials = m}
    )
  }

  submit(values) {
    this.subscribe(
      this.mixService.saveWithUnits(values.name, values.recipeId, values.materialTypeId, values.mixMaterials, values.units),
      {next: () => this.urlUtils.navigateTo(`/color/edit/${this.recipeId}`)}
    )
  }
}
