// angular
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Inject } from '@angular/core';
import { UntypedFormBuilder } from '@angular/forms';
import { UntypedFormGroup } from '@angular/forms';
import { Validators } from '@angular/forms';

// libs
import Swal from 'sweetalert2';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';

// app
import { ApiRoutes } from '../../../api-routes';
import { AccountModel } from '../../../model/account.model';
import { MatDialogRef } from '@angular/material/dialog';
import { CurrencyService } from '../../../services/currency.service';
import { CurrencyModel } from '../../../model/currency.model';
import { AccountCategoryService } from '../../../services/account.category.service';
import { AccountCategoryModel } from '../../../model/account.category.model';
import { AccountAccessRightModel } from '../../../model/account.accessRight.model';
import { AccountService } from '../../../services/account.service';

@Component({
	templateUrl: './edit.account.component.html',
	styleUrls: ['./edit.account.component.scss'],
})
export class SettingsAccountEditComponent {
	public editForm!: UntypedFormGroup;
	public errorMessage?: string;
	public currencies?: CurrencyModel[];
	public categories?: AccountCategoryModel[];
	public types = new Map<string, string>();

	constructor(
		private fb: UntypedFormBuilder,
		private httpClient: HttpClient,
		private dialogRef: MatDialogRef<SettingsAccountEditComponent>,
		private currencyService: CurrencyService,
		private accountCategoryService: AccountCategoryService,
		private accountService: AccountService,
		@Inject(MAT_DIALOG_DATA) public account: AccountModel
	) {
		this.types = this.accountService.getTypes();
	}

	ngOnInit(): void {
		this.editForm = this.fb.group({
			id: [
				this.account.id,
			],
			name: [
				this.account.name,
				[Validators.required],
			],
			currencyId: [
				this.account.currencyId,
				[Validators.required],
			],
			categoryId: [
				'',
			],
			type: [
				this.account.type,
			],
		});

		this.currencyService
			.getCurrencies()
			.subscribe(x => this.currencies = x);

		this.accountCategoryService
			.getCategories()
			.subscribe(x => this.categories = x);
	}

	removeCategory(category: AccountCategoryModel) {
		var url = ApiRoutes.SettingsAccountAccountCategory
			.replace(':id', this.account.id!)
			.replace(':categoryId', category.id!);

		this.httpClient
			.delete(url)
			.subscribe(response => {
				this.dialogRef.close({ refresh: true });
			}, error => {
				this.errorMessage = error.detail;
			});
	}

	removeAccessRight(accessRight: AccountAccessRightModel) {

	}

	closeDialog(): void {
		this.dialogRef.close();
	}

	onSubmitClick() {
		if (this.editForm.valid) {
			this.errorMessage = undefined;
			this.httpClient
				.put<AccountModel>(ApiRoutes.SettingsAccount.replace(':id', this.account.id!), this.editForm.value)
				.subscribe(response => {
					this.dialogRef.close({ refresh: true });
				}, error => {
					this.errorMessage = error.detail;
				});
		}
	}
}
