import "./add-secret-dialog.scss";

import React from "react";
import { observable } from "mobx";
import { observer } from "mobx-react";
import { t, Trans } from "@lingui/macro";
import { _i18n } from "../../i18n";
import { Dialog, DialogProps } from "../dialog";
import { Wizard, WizardStep } from "../wizard";
import { Input } from "../input";
import { systemName } from "../input/input.validators";
import { Secret, secretsApi, SecretType } from "../../api/endpoints";
import { SubTitle } from "../layout/sub-title";
import { NamespaceSelect } from "../+namespaces/namespace-select";
import { Select, SelectOption } from "../select";
import { Icon } from "../icon";
import { KubeObjectMetadata } from "../../api/kube-object";
import { base64 } from "../../utils";
import { Notifications } from "../notifications";
import { showDetails } from "../../navigation";
import upperFirst from "lodash/upperFirst";

interface Props extends Partial<DialogProps> {
}

interface SecretTemplateField {
  key: string;
  value?: string;
  required?: boolean;
}

interface SecretTemplate {
  [field: string]: SecretTemplateField[];
  annotations?: SecretTemplateField[];
  labels?: SecretTemplateField[];
  data?: SecretTemplateField[];
}

type SecretField = keyof SecretTemplate;

type RecursivePartial<T> = {
  [P in keyof T]?:
  T[P] extends (infer U)[] ? RecursivePartial<U>[] :
  T[P] extends object ? RecursivePartial<T[P]> :
  T[P];
};

@observer
export class AddSecretDialog extends React.Component<Props> {
  @observable static isOpen = false;

  static open(): void {
    AddSecretDialog.isOpen = true;
  }

  static close(): void {
    AddSecretDialog.isOpen = false;
  }

  private secretTemplate: Partial<Record<SecretType, SecretTemplate>> = {
    [SecretType.Opaque]: {},
    [SecretType.ServiceAccountToken]: {
      annotations: [
        { key: "kubernetes.io/service-account.name", required: true },
        { key: "kubernetes.io/service-account.uid", required: true }
      ],
    },
  }

  get types(): SecretType[] {
    return Object.keys(this.secretTemplate) as SecretType[];
  }

  @observable secret = this.secretTemplate;
  @observable name = "";
  @observable namespace = "default";
  @observable type = SecretType.Opaque;

  reset = (): void => {
    this.name = "";
    this.secret = this.secretTemplate;
  }

  close = (): void => {
    AddSecretDialog.close();
  }

  private getDataFromFields = (fields: SecretTemplateField[] = [], processValue?: (val: string) => string): any => {
    return fields.reduce<any>((data, field) => {
      const { key, value } = field;
      if (key) {
        data[key] = processValue ? processValue(value) : value;
      }
      return data;
    }, {});
  }

  createSecret = async (): Promise<void> => {
    const { name, namespace, type } = this;
    const { data = [], labels = [], annotations = [] } = this.secret[type];
    const metadata: Partial<KubeObjectMetadata> = {
      name,
      namespace,
      annotations: this.getDataFromFields(annotations),
      labels: this.getDataFromFields(labels),
    };
    const secret: Partial<Secret> = {
      type: type,
      data: this.getDataFromFields(data, val => val ? base64.encode(val) : ""),
      metadata: metadata as KubeObjectMetadata,
    };
    try {
      const newSecret = await secretsApi.create({ namespace, name }, secret);
      showDetails(newSecret.selfLink);
      this.reset();
      this.close();
    } catch (err) {
      Notifications.error(err);
    }
  }

  addField = (field: SecretField): void => {
    const fields = this.secret[this.type][field] || [];
    fields.push({ key: "", value: "" });
    this.secret[this.type][field] = fields;
  }

  removeField = (field: SecretField, index: number): void => {
    const fields = this.secret[this.type][field] || [];
    fields.splice(index, 1);
  }

  renderFields(field: SecretField): JSX.Element {
    const fields = this.secret[this.type][field] || [];
    return (
      <>
        <SubTitle compact className="fields-title" title={upperFirst(field.toString())}>
          <Icon
            small
            tooltip={_i18n._(t`Add field`)}
            material="add_circle_outline"
            onClick={(): void => this.addField(field)}
          />
        </SubTitle>
        <div className="secret-fields">
          {fields.map((item, index) => {
            const { key = "", value = "", required } = item;
            return (
              <div key={index} className="secret-field flex gaps auto align-center">
                <Input
                  className="key"
                  placeholder={_i18n._(t`Name`)}
                  title={key}
                  tabIndex={required ? -1 : 0}
                  readOnly={required}
                  value={key} onChange={(v): void => {
                    item.key = v;
                  }}
                />
                <Input
                  multiLine maxRows={5}
                  required={required}
                  className="value"
                  placeholder={_i18n._(t`Value`)}
                  value={value} onChange={(v): void => {
                    item.value = v;
                  }}
                />
                <Icon
                  small
                  disabled={required}
                  tooltip={required ? <Trans>Required field</Trans> : <Trans>Remove field</Trans>}
                  className="remove-icon"
                  material="remove_circle_outline"
                  onClick={(): void => this.removeField(field, index)}
                />
              </div>
            );
          })}
        </div>
      </>
    );
  }

  render(): JSX.Element {
    const { ...dialogProps } = this.props;
    const { namespace, name, type } = this;
    const header = <h5><Trans>Create Secret</Trans></h5>;
    return (
      <Dialog
        {...dialogProps}
        className="AddSecretDialog"
        isOpen={AddSecretDialog.isOpen}
        close={this.close}
      >
        <Wizard header={header} done={this.close}>
          <WizardStep contentClass="flow column" nextLabel={<Trans>Create</Trans>} next={this.createSecret}>
            <div className="secret-name">
              <SubTitle title={<Trans>Secret name</Trans>}/>
              <Input
                autoFocus required
                placeholder={_i18n._(t`Name`)}
                validators={systemName}
                value={name} onChange={(v): void => {
                  this.name = v;
                }}
              />
            </div>
            <div className="flex auto gaps">
              <div className="secret-namespace">
                <SubTitle title={<Trans>Namespace</Trans>}/>
                <NamespaceSelect
                  themeName="light"
                  value={namespace}
                  onChange={({ value }): void => this.namespace = value}
                />
              </div>
              <div className="secret-type">
                <SubTitle title={<Trans>Secret type</Trans>}/>
                <Select
                  themeName="light"
                  options={this.types}
                  value={type} onChange={({ value }: SelectOption): void => this.type = value}
                />
              </div>
            </div>
            {this.renderFields("annotations")}
            {this.renderFields("labels")}
            {this.renderFields("data")}
          </WizardStep>
        </Wizard>
      </Dialog>
    );
  }
}
