/*
Copyright 2020 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { useEffect, useRef } from "react";
import Tile from "./Tile";
import Button from "./Button";
import TextButton from "./TextButton";
import Input from "./Input";
import { Formik, Form } from "formik";
import * as Yup from "yup";

import "./CreateLinkTile.scss";

interface ILinkNotCreatedTileProps {
    setLink: React.Dispatch<React.SetStateAction<string>>;
}

const LinkNotCreatedTile = (props: ILinkNotCreatedTileProps) => {
    return (
        <Tile className="createLinkTile">
            <h1>
                Create shareable links to Matrix rooms, users or messages
                without being tied to any app
            </h1>
            <Formik
                initialValues={{
                    identifier: "",
                }}
                validationSchema={Yup.object({
                    identifier: Yup.string()
                        .test(
                            "is-identifier",
                            "That link doesn't look right. Double check the details.",
                            (link) => link
                        )
                        .required("Required"),
                })}
                onSubmit={(values) => {
                    props.setLink(
                        document.location.protocol +
                            "//" +
                            document.location.host +
                            "/" +
                            values.identifier
                    );
                }}
            >
                <Form>
                    <Input
                        name={"identifier"}
                        type={"text"}
                        placeholder="#room:example.com, @user:example.com"
                    />
                    <Button type="submit">Get Link</Button>
                </Form>
            </Formik>
        </Tile>
    );
};

interface ILinkCreatedTileProps {
    link: string;
    setLink: React.Dispatch<React.SetStateAction<string>>;
}

const LinkCreatedTile: React.FC<ILinkCreatedTileProps> = (props) => {
    const buttonRef = useRef<HTMLButtonElement>(null);

    // Focus button on render
    useEffect(() => {
        if (buttonRef && buttonRef.current) {
            buttonRef.current.focus();
        }
    });

    return (
        <Tile className="createLinkTile">
            <TextButton onClick={() => props.setLink("")}>
                Create another lnk
            </TextButton>
            <h1>{props.link}</h1>
            <Button
                flashChildren={"Copied"}
                onClick={() => navigator.clipboard.writeText(props.link)}
                ref={buttonRef}
            >
                Copy Link
            </Button>
        </Tile>
    );
};

const CreateLinkTile: React.FC = () => {
    const [link, setLink] = React.useState("");
    console.log(link);
    if (!link) {
        return <LinkNotCreatedTile setLink={setLink} />;
    } else {
        return <LinkCreatedTile link={link} setLink={setLink} />;
    }
};

export default CreateLinkTile;
