import { Component, OnInit } from '@angular/core';
import { DetailsFormBase } from '@objects-view/details-form-base';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { Observable, of } from 'rxjs';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { IContainerModel } from '@fleio-api/openstack/object-store/model/container.model';
import { ContainersApiService } from '@fleio-api/openstack/object-store/containers/containers-api.service';
import { IAccountCapabilitiesModel } from '@fleio-api/openstack/object-store/model/account-capabilities.model';
import { NotificationService } from '@shared/ui-api/notification.service';

@Component({
  selector: 'app-container-edit-form',
  templateUrl: './container-edit-form.component.html',
  styleUrls: ['./container-edit-form.component.scss']
})
export class ContainerEditFormComponent extends DetailsFormBase<IContainerModel> implements OnInit {
  containerForm: UntypedFormGroup;

  policies: {
    default: boolean;
    name: string;
  }[];
  maxContainerNameLength = 255;

  constructor(
    private formBuilder: UntypedFormBuilder,
    private containersApiService: ContainersApiService,
    private router: Router,
    private config: ConfigService,
    private notificationService: NotificationService,
  ) {
    super();
  }

  createContainerForm(): UntypedFormGroup {
    return this.formBuilder.group({
      name: ['', [Validators.required, Validators.maxLength(this.maxContainerNameLength)]],
      storage_policy: [null],
      public: [false],
    });
  }

  ngOnInit() {
    super.ngOnInit();
    if (this.objectController) {
      this.objectController.actionCallback = () => this.containerActions();
      const capabilities: IAccountCapabilitiesModel = this.objectController.additionalObjects?.capabilities;
      if (capabilities) {
        this.policies = capabilities?.swift?.policies;
        this.maxContainerNameLength = capabilities.swift.max_container_name_length || 255;
      }
    }

    this.containerForm = this.createContainerForm();

    if (this.object?.id) {
      this.containerForm.patchValue(this.object);
    } else {
      if (this.policies) {
        const defaultPolicy = this.policies.find(p => p.default);
        if (defaultPolicy) {
          this.containerForm.controls.storage_policy.setValue(defaultPolicy.name);
        }
      }
    }

  }

  containerActions(): Observable<IActionResult> {
    const container = this.containerForm.value as IContainerModel;

    this.createOrUpdate(
      this.containersApiService,
      container,
    ).subscribe(() => {
      this.notificationService.showMessage(this.object?.id ? $localize`Container updated` : $localize`Container created`);
      const parsedUrl = this.router.parseUrl(this.config.getPrevUrl('openstack/object-store'));
      parsedUrl.queryParams = {};
      this.router.navigate([decodeURI(parsedUrl.toString())], {queryParamsHandling: 'merge'},
      ).catch(() => {
      });
    });

    return of(null);
  }
}
