import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { UserManagementService } from 'src/app/services/user.management.service';

@Component({
    selector: 'page-user-create',
    templateUrl: './users.create.page.html',
    styleUrls: ['./users.create.page.scss']
})
export class PageUserCreate implements OnInit {

    form: FormGroup;
    isAdmin: boolean = false;
    working: boolean = false;

    constructor(
        private userManagementService: UserManagementService,
        private formBuilder: FormBuilder,
        private router: Router) { }

    ngOnInit(): void {
        this.form = this.formBuilder.group({
            username: ['', Validators.required],
            name: ['', Validators.nullValidator],
            email: ['', Validators.nullValidator],
            password: ['', Validators.required]
        });
    }

    createUser() {
        this.working = true;
        const user = {
            username: this.form.get("username").value,
            name: this.form.get("name").value,
            email: this.form.get("email").value,
            roles: this.isAdmin ? ['ROLE_ADMIN'] : []
        }

        const request = this.form.get("username").disabled ? this.userManagementService.update(user) : this.userManagementService.create(user);

        request.subscribe({
            next: (result: any) => {
                this.userManagementService.setPassword(result.username, this.form.get("password").value).subscribe({
                    next: (result) => {
                        this.working = false;
                        this.router.navigateByUrl('/u');
                    },
                    error: (error) => {
                        this.form.get("username").disable();

                        this.working = false;
                        if (error.status == 409) {
                            let errors = {};
                            for (let code of error.error) {
                                errors[code.field] = errors[code.field] || {};
                                errors[code.field][code.code] = true;
                            }

                            for (let code in errors) {
                                this.form.get(code).setErrors(errors[code]);
                            }
                        }
                    }
                });
            },
            error: (error) => {
                this.working = false;
                if (error.status == 409) {
                    let errors = {};
                    for (let code of error.error) {
                        errors[code.field] = errors[code.field] || {};
                        errors[code.field][code.code] = true;
                    }

                    for (let code in errors) {
                        this.form.get(code).setErrors(errors[code]);
                    }
                }
            }
        })
    }

}
