import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { IBaseDialog } from '@shared/common-dialogs/base-dialog';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { IAction } from '@objects-view/interfaces/actions/action';
import { DialogCallbackAction } from '@objects-view/actions/dialog-callback-action';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { IDbaasInstanceModel } from '@fleio-api/openstack/model/dbaas-instance.model';
import { DbaasInstancesApiService } from '@fleio-api/openstack/dbaas/dbaas-instances-api.service';


const AVAILABLE_CHARSETS = {
    armscii8: {
      display: $localize`ARMSCII-8 Armenian`,
      default_collation: 'armscii8_general_ci',
    },
    ascii: {
      display: $localize`US ASCII`,
      default_collation: 'ascii_general_ci',
    },
    big5: {
      display: $localize`Big5 Traditional Chinese`,
      default_collation: 'big5_chinese_ci',
    },
    binary: {
      display: $localize`Binary pseudo charset`,
      default_collation: 'binary',
    },
    cp1250: {
      display: $localize`Windows Central European`,
      default_collation: 'cp1250_general_ci',
    },
    cp1251: {
      display: $localize`Windows Cyrillic`,
      default_collation: 'cp1251_general_ci',
    },
    cp1256: {
      display: $localize`Windows Arabic`,
      default_collation: 'cp1256_general_ci',
    },
    cp1257: {
      display: $localize`Windows Baltic`,
      default_collation: 'cp1257_general_ci',
    },
    cp850: {
      display: $localize`DOS West European`,
      default_collation: 'cp850_general_ci',
    },
    cp852: {
      display: $localize`DOS Central European`,
      default_collation: 'cp852_general_ci',
    },
    cp866: {
      display: $localize`DOS Russian`,
      default_collation: 'cp866_general_ci',
    },
    cp932: {
      display: $localize`SJIS for Windows Japanese`,
      default_collation: 'cp932_japanese_ci',
    },
    dec8: {
      display: $localize`DEC West European`,
      default_collation: 'dec8_swedish_ci',
    },
    eucjpms: {
      display: $localize`UJIS for Windows Japanese`,
      default_collation: 'eucjpms_japanese_ci',
    },
    euckr: {
      display: $localize`EUC-KR Korean`,
      default_collation: 'euckr_korean_ci',
    },
    gb18030: {
      display: $localize`China National Standard GB18030`,
      default_collation: 'gb18030_chinese_ci',
    },
    gb2312: {
      display: $localize`GB2312 Simplified Chinese`,
      default_collation: 'gb2312_chinese_ci',
    },
    gbk: {
      display: $localize`GBK Simplified Chinese`,
      default_collation: 'gbk_chinese_ci',
    },
    geostd8: {
      display: $localize`GEOSTD8 Georgian`,
      default_collation: 'geostd8_general_ci',
    },
    greek: {
      display: $localize`ISO 8859-7 Greek`,
      default_collation: 'greek_general_ci',
    },
    hebrew: {
      display: $localize`ISO 8859-8 Hebrew`,
      default_collation: 'hebrew_general_ci',
    },
    hp8: {
      display: $localize`HP West European`,
      default_collation: 'hp8_english_ci',
    },
    keybcs2: {
      display: $localize`DOS Kamenicky Czech-Slovak`,
      default_collation: 'keybcs2_general_ci',
    },
    koi8r: {
      display: $localize`KOI8-R Relcom Russian`,
      default_collation: 'koi8r_general_ci',
    },
    koi8u: {
      display: $localize`KOI8-U Ukrainian`,
      default_collation: 'koi8u_general_ci',
    },
    latin1: {
      display: $localize`cp1252 West European`,
      default_collation: 'latin1_swedish_ci',
    },
    latin2: {
      display: $localize`ISO 8859-2 Central European`,
      default_collation: 'latin2_general_ci',
    },
    latin5: {
      display: $localize`ISO 8859-9 Turkish`,
      default_collation: 'latin5_turkish_ci',
    },
    latin7: {
      display: $localize`ISO 8859-13 Baltic`,
      default_collation: 'latin7_general_ci',
    },
    macce: {
      display: $localize`Mac Central European`,
      default_collation: 'macce_general_ci',
    },
    macroman: {
      display: $localize`Mac West European`,
      default_collation: 'macroman_general_ci',
    },
    sjis: {
      display: $localize`Shift-JIS Japanese`,
      default_collation: 'sjis_japanese_ci',
    },
    swe7: {
      display: $localize`7bit Swedish`,
      default_collation: 'swe7_swedish_ci',
    },
    tis620: {
      display: $localize`TIS620 Thai`,
      default_collation: 'tis620_thai_ci',
    },
    ucs2: {
      display: $localize`UCS-2 Unicode`,
      default_collation: 'ucs2_general_ci',
    },
    ujis: {
      display: $localize`EUC-JP Japanese`,
      default_collation: 'ujis_japanese_ci',
    },
    utf16: {
      display: $localize`UTF-16 Unicode`,
      default_collation: 'utf16_general_ci',
    },
    utf16le: {
      display: $localize`UTF-16LE Unicode`,
      default_collation: 'utf16le_general_ci',
    },
    utf32: {
      display: $localize`UTF-32 Unicode`,
      default_collation: 'utf32_general_ci',
    },
    utf8: {
      display: $localize`UTF-8 Unicode`,
      default_collation: 'utf8_general_ci',
    },
    utf8mb4: {
      display: $localize`UTF-8 Unicode`,
      default_collation: 'utf8mb4_0900_ai_ci',
    },
  } as Record<string, Record<string, string>>;

@Component({
  selector: 'app-create-database-dialog',
  templateUrl: './create-database-dialog.component.html',
  styleUrls: ['./create-database-dialog.component.scss']
})
export class CreateDatabaseDialogComponent implements IBaseDialog, OnInit {
  @ViewChild('formErrors') formErrors;
  createDatabaseForm = this.formBuilder.group({
    name: ['', Validators.required],
    character_set: ['utf8'],
    collate: ['utf8_general_ci'],
  });
  dialogTitle = $localize`Create database`;
  dialogActions: Array<IAction> = [
    new DialogCallbackAction({
      name: $localize`Create database`,
      primary: true,
      callback: () => this.createDatabase(),
    }),
  ];
  loading = false;
  dialogForm = this.createDatabaseForm;
  availableCharsets: Record<string, Record<string, string>> = AVAILABLE_CHARSETS;
  availableCharsetKeys = Object.keys(this.availableCharsets);

  constructor(
    public dialogRef: MatDialogRef<CreateDatabaseDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: {
      database_instance: IDbaasInstanceModel;
    },
    private dbaasInstancesApiService: DbaasInstancesApiService,
    private formBuilder: UntypedFormBuilder,
  ) {
  }

  ngOnInit() {
    this.createDatabaseForm.controls.character_set.valueChanges.subscribe(value => {
      this.createDatabaseForm.controls.collate.setValue(
        this.availableCharsets[value].default_collation
      );
    });
  }


  createDatabase() {
    if (!this.createDatabaseForm.valid) {
      return;
    }
    const database = this.createDatabaseForm.value;
    this.loading = true;
    this.dbaasInstancesApiService.createDatabase(
      this.data.database_instance.id,
      database.name,
      database.character_set,
      database.collate,
    ).subscribe({
      next: () => {
        this.dialogRef.close(true);
      }, error: error => {
        this.formErrors.setBackendErrors(error.error);
      }
    }).add(() => {
      this.loading = false;
    });
  }

}
