import React from "react";
import Helmet from "react-helmet";
import { Markdown, styled } from "@patternplate/components";
import tag from "tag-hoc";
import Transition from "react-transition-group/Transition";

import PatternDemo from "./pattern-demo";

const VISIBILITY = props => (props.checkers ? "block" : "none");

const StyledPattern = styled(tag(["checkers"])("div"))`
  box-sizing: border-box;
  height: 100%;
  width: 100%;
  &::before {
    content: "";
    display: ${VISIBILITY};
    position: absolute;
    z-index: 1;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: ${props => checkers(props)};
    background-size: 16px 16px;
    background-position: 0 0, 8px 8px;
  }
`;

const StyledPatternFolder = styled.div`
  height: 100%;
  width: 100%;
`;

const StyledPatternDoc = styled.div`
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 30px;
  box-sizing: border-box;
`;

const StyledPatternDemo = styled.div`
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  max-width: 1240px;
  margin: 0 auto;
`;

const StyledPatternLoader = styled.div`
  position: absolute;
  z-index: 3;
  top: 0;
  right: 0;
  left: 0;
  height: 3px;
  &::after {
    position: absolute;
    top: 0;
    z-index: 2;
    content: "";
    display: block;
    width: 100%;
    height: 100%;
    background: ${props =>
      props.error ? props.theme.error : props.theme.active};
    opacity: 1;
    transition: ${props => (props.error ? "none" : "transform 1s ease-in-out")};
    ${props => {
      switch (props.status) {
        case "entering":
          return `
            transform: translateX(-100%);
          `;
        case "entered":
          return `${props =>
            props.error ? "" : "transform: translateX(-15%);"}`;
        case "exiting":
          return `
            transition: transform .3s ease-out;
            transform: translateX(0);
          `;
        case "exited":
        default:
          return `
            transform: translateX(-100%);
            transition: opacity .3s .25s ease-out;
          `;
      }
    }};
  }
`;

export default class Pattern extends React.Component {
  constructor(...args) {
    super(...args);
    this.state = { srcdoc: false };
  }

  render() {
    const { props } = this;

    if (props.contentType !== "pattern") {
      return null;
    }

    if (props.type === "folder") {
      return (
        <StyledPatternFolder>
          <StyledPatternDoc>
            <Markdown source={props.docs} />
          </StyledPatternDoc>
        </StyledPatternFolder>
      );
    }

    return (
      <StyledPattern checkers={props.opacity}>
        <Helmet
          title={[getPrefix(props), props.displayName]
            .filter(Boolean)
            .join(": ")}
        />
        <Transition
          in={props.loading || props.error}
          timeout={{ enter: 1000, exit: 850 }}
        >
          {status => (
            <StyledPatternLoader status={status} error={props.error} />
          )}
        </Transition>
        <StyledPatternDemo>
          <PatternDemo
            src={props.src}
            contents={props.contents}
            loading={props.loading}
            updated={props.updated}
          />
        </StyledPatternDemo>
      </StyledPattern>
    );
  }
}

function grad(fill) {
  return `linear-gradient(45deg, ${fill} 25%, transparent 25%, transparent 75%, ${fill} 75%, ${fill})`;
}

function checkers(props) {
  const fill = props.theme.border;
  return `
    ${grad(fill)},
    ${grad(fill)};
  `;
}

function getPrefix(props) {
  if (props.loading) {
    return "Load";
  }
  if (props.error) {
    return "Error";
  }
  return "";
}

@@ -311,16 +311,210 @@
 %22none%22);
+%0Aconst CROSSES = props =%3E encodeURI(%60%3Csvg width=%2260%22 height=%2260%22 xmlns=%22http://www.w3.org/2000/svg%22%3E%3Cpath stroke-width=%221.5%22 stroke=%22#666666%22 fill=%22none%22 d=%22M15 10.187v9.875M10 15h10%22/%3E%3C/svg%3E%60);
 %0A%0Aconst 
@@ -828,37 +828,58 @@
 ound
-: $%7Bprops =%3E checkers(props)%7D
+-image: url(%22data:image/svg+xml;utf-8,$%7BCROSSES%7D%22)
 ;%0A  
@@ -902,14 +902,14 @@
 e: 1
-6
+5
 px 1
-6
+5
 px;%0A
@@ -927,30 +927,56 @@
 und-
-position: 0 0, 8px 8px
+repeat: repeat;%0A    background-position: 50%25 50%25
 ;%0A  
@@ -3584,255 +3584,8 @@
 %0A%7D%0A%0A
-function grad(fill) %7B%0A  return %60linear-gradient(45deg, $%7Bfill%7D 25%25, transparent 25%25, transparent 75%25, $%7Bfill%7D 75%25, $%7Bfill%7D)%60;%0A%7D%0A%0Afunction checkers(props) %7B%0A  const fill = props.theme.border;%0A  return %60%0A    $%7Bgrad(fill)%7D,%0A    $%7Bgrad(fill)%7D;%0A  %60;%0A%7D%0A
 %0Afun

make opacity indicator nicer