import * as Cookie from "js-cookie";
import * as React from "react";
import GlobalToast from "./index";

const { Component } = React;

export type ToastType = "error" | "info" | "success" | "warning";

export interface IToastState {
  message: string;
  url: string;
  buttonLabel: string;
  type: ToastType;
  duration: number;
  animationDuration: number;
  visible: boolean;
  title: string;
}

export default class GlobalToastFromCookie extends React.Component<{}, IToastState> {
  constructor(props) {
    super(props);

    this.state = {
      message: "",
      url: "",
      buttonLabel: "",
      type: "success",
      duration: 5000,
      animationDuration: 200,
      visible: false,
      title: "",
    };
  }
  public componentDidMount() {
    const toastCookie = Cookie.get("lpToast");
    if (toastCookie) {
      return this.setState({
        ...this.state,
        ...JSON.parse(toastCookie),
        visible: true,
      });
    }
  }

  public render() {
    return(
      <GlobalToast
        message={this.state.message}
        buttonLabel={this.state.buttonLabel}
        url={this.state.url}
        type={this.state.type}
        duration={this.state.duration}
        animationDuration={this.state.animationDuration}
        visible={this.state.visible}
        title={this.state.title}
        onClose={() => {
          Cookie.remove("lpToast", { domain: ".lonelyplanet.com"});
        }}
      />
    );
  }
}
