// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import React, { useEffect, useState } from 'react';
import PropertyFilter from "@cloudscape-design/components/property-filter";
import { useCollection } from '@cloudscape-design/collection-hooks';
import { COLUMN_DEFINITIONS, FILTERING_PROPERTIES, PROPERTY_FILTERING_I18N_CONSTANTS, DEFAULT_PREFERENCES, Preferences } from './input-file-table-config';
import {
  BreadcrumbGroup,
  Button,
  HelpPanel,
  Pagination,
  SpaceBetween,
  Table,
} from '@cloudscape-design/components';
import { CustomAppLayout } from '../common/app-layout';
import { Navigation } from '../common/navigation';
import { Notifications } from '../common/notifications';
import { TableEmptyState, TableHeader, TableNoMatchState } from '../common/table-components';

import { paginationLabels, distributionSelectionLabels } from '../../common/labels';
import { getFilterCounterText } from '../../common/tableCounterStrings';
import '../../styles/base.scss';
import { useColumnWidths } from '../common/use-column-widths';
import { useLocalStorage } from '../../common/localStorage';
import { ICustomerFile, ReduxRoot } from "../../interfaces";

import { useDispatch, useSelector } from "react-redux";
import { storeCustomerFile } from "../../redux/actions";
import { useHistory } from "react-router-dom";
import { getCustomerInputFiles, getFileContent } from "../../data";

export const resourcesBreadcrumbs = [
  {
    text: 'Input Files',
    href: '/InputFiles',
  }
];

export const Breadcrumbs = () => (
    <BreadcrumbGroup items={resourcesBreadcrumbs} expandAriaLabel="Show path" ariaLabel="Breadcrumbs" />
);

export const FullPageHeader = ({
                                 resourceName = 'Input Files',
                                 createButtonText = 'Create input file',
                                 downloadFile,
                                 ...props
                               }) => {
  const isOnlyOneSelected = props.selectedItems.length === 1;

  const history = useHistory();

  const onOpenClick = () => {
    history.push("/InputFile");
  }

  const onDownload = () => {
    downloadFile()
  }

  const onUpload = () => {
    history.push("/InputFileForm");
  }

  return (
      <TableHeader
          variant="awsui-h1-sticky"
          title={resourceName}
          actionButtons={
            <SpaceBetween size="xs" direction="horizontal">
              <Button onClick={onOpenClick} disabled={!isOnlyOneSelected}>View details</Button>
              <Button onClick={onDownload} disabled={!isOnlyOneSelected}>Download</Button>
              <Button onClick={onUpload}>Upload</Button>
            </SpaceBetween>
          }
          {...props}
      />
  );
};

export const ToolsContent = () => (
    <HelpPanel
        header={<h2>Input Files</h2>}
        footer={
          <>
          </>
        }
    >
      <p>
        View all the input files.
      </p>
    </HelpPanel>
);

function TableContent({updateTools}) {

  const dispatch = useDispatch();

  const token = useSelector( (state:ReduxRoot) => {
    return state.reducerState.token
  });

  const [files, setFiles] = useState([]);
  const [selectedFiles, setSelectedFiles] = useState([]);
  const [selectedInputFile, setSelectedInputFile] = useState();

  const [columnDefinitions, saveWidths] = useColumnWidths('React-Table-Widths', COLUMN_DEFINITIONS);
  const [preferences, setPreferences] = useLocalStorage('React-DistributionsTable-Preferences', DEFAULT_PREFERENCES);

  const { items, actions, filteredItemsCount, collectionProps, paginationProps, propertyFilterProps } = useCollection(
      files,
    {
      propertyFiltering: {
        filteringProperties: FILTERING_PROPERTIES,
        empty: <TableEmptyState resourceName="Input File" />,
        noMatch: (
            <TableNoMatchState
                onClearFilter={() => {
                  actions.setPropertyFiltering({ tokens: [], operation: 'and' });
                }}
            />
        ),
      },
      pagination: { pageSize: preferences.pageSize },
      sorting: { defaultState: { sortingColumn: columnDefinitions[0] } },
      selection: {},
    }
  );

  const getAllInputFiles = async () => {

    try {

      await getCustomerInputFiles(token).then(
          (result: ICustomerFile[]) => {
            setFiles(result);
          });

      await Promise.resolve();

    }
    catch (err) {
      console.log("Got Error Message: " + err.toString());
    }
  }

  useEffect( () => {

    getAllInputFiles().then(() => console.log("getAllInputFiles() completed."));
  }, []);

  const selectFile = (customerFile: ICustomerFile) => {
    dispatch(storeCustomerFile(customerFile?customerFile: {}));
  }

  const downloadFile = async () => {
    await getFileContent(token, selectedFiles[0].name).then(
        (content: string) => {
          console.log("Download Called : " + selectedFiles[0].name)
          const element = document.createElement("a");
          const file = new Blob([content], {type: 'text/csv;charset=utf-8'});
          element.href = URL.createObjectURL(file);
          const datestamp = new Date().toLocaleDateString()
          const timestamp = new Date().toLocaleTimeString()
          element.download = selectedFiles[0].name.concat('-').concat(datestamp).concat('-').concat(timestamp).concat('.csv');
          document.body.appendChild(element);
          element.click();
        });

    await Promise.resolve();
  }

  return (
    <Table
      {...collectionProps}
      items={items}
      columnDefinitions={columnDefinitions}
      visibleColumns={preferences.visibleContent}
      ariaLabels={distributionSelectionLabels}
      selectionType="single"
      variant="full-page"
      stickyHeader={true}
      resizableColumns={true}
      wrapLines={preferences.wrapLines}
      onColumnWidthsChange={saveWidths}
      header={
        <FullPageHeader
          selectedItems={selectedFiles}
          totalItems={files}
          updateTools={updateTools}
          serverSide={false}
          downloadFile={downloadFile}
        />
      }
      loadingText="Loading input files"
      filter={
        <PropertyFilter
          i18nStrings={PROPERTY_FILTERING_I18N_CONSTANTS}
          {...propertyFilterProps}
          countText={getFilterCounterText(filteredItemsCount)}
          expandToViewport={true}
        />
      }
      pagination={<Pagination {...paginationProps} ariaLabels={paginationLabels} />}
      preferences={<Preferences preferences={preferences} setPreferences={setPreferences} />}
      selectedItems={selectedFiles}
      onSelectionChange={evt => {setSelectedFiles(evt.detail.selectedItems); selectFile(evt.detail.selectedItems[0])}}
    />
  );
}

function InputFileTableView() {
  const [columnDefinitions, saveWidths] = useColumnWidths('React-TableServerSide-Widths', COLUMN_DEFINITIONS);
  const [toolsOpen, setToolsOpen] = useState(false);

  return (
    <CustomAppLayout
      navigation={<Navigation activeHref="/InputFiles" />}
      notifications={<Notifications successNotification={false} />}
      breadcrumbs={<Breadcrumbs />}
      content={
        <TableContent
          columnDefinitions={columnDefinitions}
          saveWidths={saveWidths}
          updateTools={() => setToolsOpen(true)}
        />
      }
      contentType="table"
      tools={<ToolsContent />}
      toolsOpen={toolsOpen}
      onToolsChange={({ detail}) => setToolsOpen(detail.open)}
      stickyNotifications={true}
    />
  );
}

export default InputFileTableView;