import * as React from 'react';
import { Maybe } from '../../../common';
import { ColumnProps } from './';
import { Node } from '../../../state';
import { Tooltip, TooltipCopyCallback } from '../../';
import icon from '../../../icons/fingerprint.svg';

export class NetworkIdColumn extends React.Component<ColumnProps> {
  public static readonly label = 'Network ID';
  public static readonly icon = icon;
  public static readonly width = 90;
  public static readonly setting = 'networkId';
  public static readonly sortBy = ({ networkId }: Node) => networkId || '';

  private data: Maybe<string>;
  private copy: Maybe<TooltipCopyCallback>;

  public shouldComponentUpdate(nextProps: ColumnProps) {
    return this.data !== nextProps.node.networkId;
  }

  render() {
    const { networkId } = this.props.node;

    this.data = networkId;

    if (!networkId) {
      return <td className="Column">-</td>;
    }

    return (
      <td className="Column" onClick={this.onClick}>
        <Tooltip text={networkId} position="left" copy={this.onCopy} />
        {networkId}
      </td>
    );
  }

  private onCopy = (copy: TooltipCopyCallback) => {
    this.copy = copy;
  };

  private onClick = (event: React.MouseEvent) => {
    event.stopPropagation();

    if (this.copy != null) {
      this.copy();
    }
  };
}
