import React, { useState } from "react";
import styled from "styled-components";

import Select, { Styles } from "react-select";

import { ISearchProviderProps } from "./searchBar";
import selectedTheme, { setTheme, IThemeProps } from "../lib/theme";
import { Button, SubHeadline } from "./elements";

import Modal from "./modal";

/**
 * Complementary code to get hover pseudo-classes working in React
 * @param color the color of the element on hover
 * @param backgroundColor the background color of the element on hover
 * @param border the border of the element on hover
 * @param borderColor the border color of the element on hover
 */
interface IHoverProps {
  color?: string;
  backgroundColor?: string;
  border?: string;
  borderColor?: string;
}

interface IPseudoProps extends React.CSSProperties {
  "&:hover": IHoverProps
}

const FormContainer = styled.div`
  display: grid;
  grid-template-columns: auto auto auto;
`;

const Table = styled.table`
  font-weight: 400;
  background: none;
  color: ${selectedTheme.mainColor};
`;

const TableRow = styled.tr`
  border-bottom: 1px solid ${selectedTheme.mainColor};
`;

const TableCell = styled.td`
  background: none;
  padding-top: 0.5rem;
`;

const HeadCell = styled.th`
  font-weight: 700;
  background: none;
`;

const Section = styled.div`
  padding: 1rem 0;
`;

const SectionHeadline = styled(SubHeadline)`
  width: 100%;
  border-bottom: 1px solid ${selectedTheme.accentColor};
  margin-bottom: 0.5rem;
`;

const SelectorStyle: Partial<Styles<IThemeProps, false>> = {
  indicatorSeparator: () => ({
    display: "none",
  }),
  container: (base: React.CSSProperties): React.CSSProperties => ({
    ...base,
    margin: "0 2px",
  }),
  dropdownIndicator: (base: React.CSSProperties): IPseudoProps => ({
    ...base,
    color: selectedTheme.mainColor,
    "&:hover": {
      color: selectedTheme.mainColor
    }
  }),
  control: (base: React.CSSProperties): IPseudoProps => ({
    ...base,
    fontWeight: 500,
    color: selectedTheme.mainColor,
    textTransform: "uppercase",
    width: "12rem",
    background: "none",
    borderRadius: 0,
    border: "1px solid",
    borderColor: selectedTheme.mainColor,
    boxShadow: "none",
    "&:hover": {
      border: "1px solid",
      borderColor: selectedTheme.mainColor
    },
  }),
  menu: (base: React.CSSProperties): React.CSSProperties => ({
    ...base,
    backgroundColor: selectedTheme.backgroundColor,
    border: "1px solid " + selectedTheme.mainColor,
    borderRadius: 0,
    boxShadow: "none",
    margin: "4px 0"
  }),
  option: (base: React.CSSProperties): IPseudoProps => ({
    ...base,
    fontWeight: 500,
    color: selectedTheme.mainColor,
    textTransform: "uppercase",
    borderRadius: 0,
    boxShadow: "none",
    backgroundColor: selectedTheme.backgroundColor,
    "&:hover": {
      backgroundColor: selectedTheme.mainColor,
      color: selectedTheme.backgroundColor,
    },
  }),
  singleValue: (base: React.CSSProperties): React.CSSProperties => ({
    ...base,
    color: selectedTheme.mainColor,
  }),
};

interface ISettingsProps {
  themes: Array<IThemeProps> | undefined;
  providers: Array<ISearchProviderProps> | undefined;
}

const Settings = ({ themes, providers }: ISettingsProps) => {
  const [newTheme, setNewTheme] = useState();

  if (themes && providers) {
    return (
      <Modal element="icon" icon="settings" title="Settings">
        {themes && (



          <Section>
            <SectionHeadline>Theme:</SectionHeadline>
            <FormContainer>
              <Select
                options={themes}
                defaultValue={selectedTheme}
                onChange={(e: any) => {
                  setNewTheme(e);
                }}
                styles={SelectorStyle}
              />
              <Button onClick={() => setTheme(JSON.stringify(newTheme))}>
                Apply
              </Button>
              <Button onClick={() => window.location.reload()}>Refresh</Button>
            </FormContainer>
          </Section>
        )}
        {providers && (

          <Section>
            <SectionHeadline>Search Providers</SectionHeadline>
            <Table>
              <tbody>
                <TableRow>
                  <HeadCell>Search Provider</HeadCell>
                  <HeadCell>Prefix</HeadCell>
                </TableRow>
                {providers.map((provider, index) => (
                  <TableRow key={provider.name + index}>
                    <TableCell>{provider.name}</TableCell>
                    <TableCell>{provider.prefix}</TableCell>
                  </TableRow>
                ))}
              </tbody>
            </Table>
          </Section>

        )}
      </Modal>
    );
  } else {
    return <></>;
  }
};

export default Settings;
