/* Copyright (C) 2019 Monomax Software Pty Ltd
 *
 * This file is part of Dnote.
 *
 * Dnote is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dnote is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Dnote.  If not, see <https://www.gnu.org/licenses/>.
 */

import classnames from 'classnames';
import { parseSearchString } from 'jslib/helpers/url';
import React, { useEffect, useState } from 'react';
import Helmet from 'react-helmet';
import { Link, RouteComponentProps, withRouter } from 'react-router-dom';
import { getLoginPath } from 'web/libs/paths';
import services from 'web/libs/services';
import Flash from '../../Common/Flash';
import Logo from '../../Icons/Logo';
import Content from './Content';
import styles from './EmailPreferenceRepetition.scss';

interface Match {
  repetitionUUID: string;
}
interface Props extends RouteComponentProps<Match> {}

const EmailPreferenceRepetition: React.FunctionComponent<Props> = ({
  location,
  match
}) => {
  const [data, setData] = useState(null);
  const [isFetching, setIsFetching] = useState(false);
  const [successMsg, setSuccessMsg] = useState('');
  const [failureMsg, setFailureMsg] = useState('');

  const { token } = parseSearchString(location.search);
  const { repetitionUUID } = match.params;

  useEffect(() => {
    if (data !== null) {
      return;
    }

    setIsFetching(true);

    services.repetitionRules
      .fetch(repetitionUUID, { token })
      .then(repetition => {
        setData(repetition);
        setIsFetching(false);
      })
      .catch(err => {
        if (err.response.status === 401) {
          setFailureMsg('Your email token has expired or is not valid.');
        } else {
          setFailureMsg(err.message);
        }

        setIsFetching(false);
      });
  }, [data, repetitionUUID, setData, setFailureMsg, setIsFetching, token]);

  const isFetched = data !== null;

  return (
    <div className={styles.wrapper}>
      <Helmet>
        <title>Toggle repetition</title>
      </Helmet>

      <Link to="/">
        <Logo fill="#252833" width={60} height={60} />
      </Link>
      <h1 className={styles.heading}>Toggle repetition</h1>

      <div className="container">
        <div className={styles.body}>
          <Flash
            when={failureMsg !== ''}
            kind="danger"
            wrapperClassName={styles.flash}
          >
            {failureMsg}{' '}
            <span>
              Please <Link to={getLoginPath()}>login</Link> and try again.
            </span>
          </Flash>

          <Flash
            when={successMsg !== ''}
            kind="success"
            wrapperClassName={classnames(styles.flash, 'T-success')}
          >
            {successMsg}
          </Flash>

          {isFetching && <div>Loading</div>}

          {isFetched && (
            <Content
              token={token}
              data={data}
              setSuccessMsg={setSuccessMsg}
              setFailureMsg={setFailureMsg}
            />
          )}
        </div>
        <div className={styles.footer}>
          <Link to="/">Back to Dnote home</Link>
        </div>
      </div>
    </div>
  );
};

export default withRouter(EmailPreferenceRepetition);
