import * as React from "react"
import styled from "styled-components"
import withTranslate from "jamplay-common/i18n/withTranslate"
import { getConfig } from "jamplay-common/remote-config"

const Container = styled.div`
  position: relative;
  width: 280px;
  min-height: 90px;
  box-sizing: border-box;
  box-shadow: rgba(0, 0, 0, 0.3) 0px 3px 8px;
  border-radius: 5px;
  cursor: pointer;
  .toast__header {
    color: white;
    padding: 3px 15px;
    border-radius: 5px 5px 0 0;
  }
  .toast__close-button {
    background-color: black;
    position: absolute;
    top: -9px;
    right: -10px;
    border-radius: 50%;
    height: 20px;
    width: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    img {
      width: 8px;
      height: 8px;
    }
  }
  .toast__header--success {
    background-color: rgb(48, 96, 61);
  }
  .toast__header--error {
    background-color: rgb(204, 53, 53);
  }
  .toast__content {
    display: flex;
    min-height: 65px;
    align-items: center;
    font-size: 1rem;
    min-width: 250px;
    padding: 0px 15px;
    border-radius: 0 0 5px 5px;
  }
  .toast__content--success {
    background-color: rgb(96, 194, 123);
    color: white;
  }
  .toast__content--error {
    background-color: rgb(255, 225, 225);
    color: rgb(204, 53, 53);
  }
  .toast__message {
    margin-left: 10px;
  }
  .toast__icon {
    min-width: 20px;
    height: 20px;
    background-size: 20px 20px;
  }
  .toast__icon--success {
    background-image: url(${getConfig().publicRuntimeConfig.staticFolder || ""}/ui-assets/images/toast/toast-success.png);
  }
  .toast__icon--error {
    background-image: url(${getConfig().publicRuntimeConfig.staticFolder || ""}/ui-assets/images/toast/toast-error.png);
  }
`

export type TProps = {
  message: string
  params?: any
  style?: any
  className?: any
  onClose?: any
}

const factory = (type): React.SFC<TProps> => {
  return withTranslate((props: TProps & { t: any }) => {
    return (
      <Container className={props.className} style={props.style} onClick={props.onClose}>
        <div className={`toast__header toast__header--${type}`}>
          {props.t(`toast/${type}-header`)}
        </div>
        <div className={`toast__content toast__content--${type}`}>
          <div className={`toast__icon toast__icon--${type}`} />
          <div className="toast__message">
            {
              props.t(props.message, props.params).split("\n").map((msg, idx) => (
                <div
                  key={idx}
                  dangerouslySetInnerHTML={{
                    __html: msg
                  }}
                />
              ))
            }
          </div>
        </div>
        <div className="toast__close-button">
          <img src={(getConfig().publicRuntimeConfig.staticFolder || "") + "/ui-assets/images/toast/toast-close.png"} />
        </div>
      </Container>
    )
  })
}

export const Success = factory("success")
export const Error = factory("error")
