import { Component, OnInit } from "@angular/core";
import {
    FormControl,
    FormGroup,
    ReactiveFormsModule,
    Validators,
} from "@angular/forms";
import { TranslateModule, TranslateService } from "@ngx-translate/core";
import { MatFormFieldModule } from "@angular/material/form-field";
import { MatInputModule } from "@angular/material/input";
import { MatButtonModule } from "@angular/material/button";

import { AddAction } from "../../models/add-action.enum";
import { ServiceComm } from "../../services/service-comm.service";
import { JumpTypeService } from "../../services/jump-type.service";

@Component({
    selector: "app-new-jump-type",
    templateUrl: "./new-jump-type.component.html",
    styleUrls: ["./new-jump-type.component.css"],
    imports: [
        TranslateModule,
        MatFormFieldModule,
        ReactiveFormsModule,
        MatInputModule,
        MatButtonModule,
    ],
})
export class NewJumpTypeComponent implements OnInit {
    public addForm: FormGroup;

    constructor(
        private serviceComm: ServiceComm,
        private jumpTypeService: JumpTypeService,
        private translateService: TranslateService,
    ) {
        this.addForm = new FormGroup(
            {
                jumptypeName: new FormControl("", Validators.required),
            },
            { updateOn: "blur" },
        );
    }

    ngOnInit() {
        this.serviceComm.forceTranslateTitle.subscribe((data) => {
            if (data === true) {
                this.updateTitle();
            }
        });

        this.updateTitle();
    }

    onSubmit(formData) {
        this.jumpTypeService
            .addJumpType(formData.jumptypeName)
            .subscribe(() => {
                this.serviceComm.refreshData(AddAction.JumpType);
            });
    }

    private updateTitle() {
        this.translateService.get("NewJumpType_Title").subscribe((data) => {
            this.serviceComm.updatedComponentTitle(data);
        });
    }
}
