import { Component, OnInit } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { first } from 'rxjs/operators';
import {AlertService, AuthenticationService, FireFormsService, UserService} from '@/_services';
import { User } from '@/_models';
import {CompanyService} from "@/_services/company.service";
import {Company} from "@/_models/company";
import {Observable} from "rxjs";
import {UsersComponent} from "@/users";


@Component({ templateUrl: 'company.component.html' })
export class CompanyComponent implements OnInit {
    registerForm: FormGroup;
    loading = false;
    submitted = false;
    currentUser: User;
    currentCompany: Company;
    users: User[];

    constructor(
        private formBuilder: FormBuilder,
        private router: Router,
        private route: ActivatedRoute,
        private authenticationService: AuthenticationService,
        private companyService: CompanyService,
        private userService: UserService,
        private alertService: AlertService
    ) {
        this.authenticationService.currentUser.subscribe(x => this.currentUser = x);
        // @ts-ignore
        this.currentCompany = this.route.snapshot.queryParams || null;
    }

    ngOnInit() {
        if (this.currentCompany.id != null) {
            this.registerForm = this.formBuilder.group({
                id: [this.currentCompany.id],
                name: [this.currentCompany.name, Validators.required],
                address: [this.currentCompany.address, Validators.required],
                phoneNumber: [this.currentCompany.phoneNumber, Validators.required],
                descriptions: [this.currentCompany.descriptions, Validators.required],
            });
        } else {
            this.registerForm = this.formBuilder.group({
                name: ['', Validators.required],
                address: ['', Validators.required],
                phoneNumber: ['', Validators.required],
                descriptions: ['', Validators.required],
            });
        }
        this.loadAllUsers();
    }

    private loadAllUsers() {
        this.userService.getAll()
            .pipe(first())
            .subscribe(users => this.users = users);
    }

    // convenience getter for easy access to form fields
    get f() { return this.registerForm.controls; }

    onSubmit() {
        this.submitted = true;

        // reset alerts on submit
        this.alertService.clear();

        // stop here if form is invalid
        if (this.registerForm.invalid) {
            return;
        }

        this.loading = true;
        this.companyService.register(this.registerForm.value)
            .pipe(first())
            .subscribe(
                data => {
                    this.alertService.success('Fire Form added successfully', true);
                    this.router.navigate(['/']);
                },
                error => {
                    this.alertService.error(error);
                    this.loading = false;
                });
    }
}
