import { Service } from '../../../interfaces/CardTypes';
import styled, { css, DefaultTheme } from 'styled-components';
import Link from 'next/link';
import Image from 'next/image';
import { useState } from 'react';

// needed for Online Status checks
interface OnlinePropType {
  status: string;
}

interface ActivePropType {
  active?: number;
}

const Card = styled.div<ActivePropType>`
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 332px;
  margin: 30px 0px;
  border: 1px solid;
  

  ${ props => {
    let ret;
    if(props.active) {
      ret =  css`
        position: relative;
        background-color: black; // DEBUG
        margin-bottom: -120px;
        height: 300px;
        z-index: 10;
      `
    }
    else {
      ret =  css`
        background-color: green; // DEBUG
        height: 180px;
      `
    }
    return ret;
  }}
`

const Test = ({ content }: { content: Service }) => {
  return (
    <h2>{content.name}</h2>
  )
}

const Test2 = styled.div<ActivePropType>`
  ${props => props.active ?
    css`
      visibility: visible;
      height: 50px;
    ` :
    css`
      visibility: hidden;
      height: 0px;
    `}
`

export const ServiceCardMobile = ({ content }: { content: Service }) => {
  const [expanded, setExpanded] = useState(false);
  let card;

  // TEMP
  if (content.href) {
    // TODO: adjust sizes
    card = (
      <Card active={+expanded} onBlur={() => setExpanded(false)}>
        <Link href={content.href}>
          <Test content={content} />
          <Test2 active={+expanded}>{content.desc}</Test2>
        </Link>
        <button onClick={() => setExpanded(expanded => !expanded)}>expand</button>
      </Card>
    )
  }
  else {
    card = <Card>
      <Test content={content} />
      <Test2>{content.desc}</Test2>
      <button onClick={() => setExpanded(expanded => !expanded)}>expand</button>
    </Card>
  }

  return card;
}

// TODO: remove unneeded exports

const CardStyle = css`
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  width: 332px;
  height: 240px;
`;
const CardLink = styled(Link)`
  ${CardStyle}
`;
const CardStyleWrap = styled.div`
  ${CardStyle}
`;
// replaces .card & .contentcard
const PageCard = styled.div`
  margin: 1rem;
  padding: 23px 10px;
  text-align: center;
  color: ${({ theme }) => theme.colors.primary};
  background-color: ${({ theme }) => theme.colors.background};
  text-decoration: none;
  border: 2px solid;
  border-radius: 10px;
  border-color: ${({ theme }) => theme.colors.primary};
  transition: all 0.1s linear;
  width: 300px;
  height: 200px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  h2 {
    margin: 0 0 1rem 0;
    font-size: 1.5rem; 
  }

  p {
    margin: 0;
    font-size: 1rem;
    line-height: 1.5;
  }

  ${CardStyleWrap}:focus,${CardStyleWrap}:active,${CardStyleWrap}:hover & {
    color: ${({ theme }) => theme.colors.secondary};
    border-color: ${({ theme }) => theme.colors.secondary};
    background-color: ${({ theme }) => theme.invertButtons ?
    theme.colors.backgroundAlt ? theme.colors.backgroundAlt : theme.colors.background : theme.colors.background};
  }

  ${CardLink}:focus,${CardLink}:active,${CardLink}:hover & {
    color: ${({ theme }) => theme.colors.secondary};
    border-color: ${({ theme }) => theme.colors.secondary};
    background-color: ${({ theme }) => theme.invertButtons ?
    theme.colors.backgroundAlt ? theme.colors.backgroundAlt : theme.colors.background : theme.colors.background};
  }
`;
// replaces the three status classes
const OnlineStatus = styled.p<OnlinePropType>`
  color: ${props => {
    let ret;
    switch (props.status) {
      case "Online":
        ret = ({ theme }: { theme: DefaultTheme }) => theme.colors.online;
        break;
      case "Loading":
        ret = ({ theme }: { theme: DefaultTheme }) => theme.colors.loading;
        break;
      case "Offline":
        ret = ({ theme }: { theme: DefaultTheme }) => theme.colors.offline;
        break;
      default:
        ret = ({ theme }: { theme: DefaultTheme }) => theme.colors.offline;
    }
    return ret;
  }};
  padding: 0.2rem;
  border: 1px solid;
  border-color: ${({ theme }) => theme.colors.primary};
  border-radius: 5px;
  width: min-content;
  position: absolute;
  top: 100; right: 50; bottom: 0; left: 50;
  offset-position: bottom 10px;
  transition: color 0.15s ease, border-color 0.15s ease;
  background-color: ${({ theme }) => theme.colors.background};
  background-image: ${({ theme }) => theme.backgroundImage ?
    "linear-gradient("
    + theme.colors.background + "," + theme.colors.background +
    "), url(" + theme.backgroundImage + ")" : ""};
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;

  ${CardStyleWrap}:focus,${CardStyleWrap}:active,${CardStyleWrap}:hover & {
    border-color: ${({ theme }) => theme.colors.secondary};
    background-color: ${({ theme }) => theme.invertButtons ?
    theme.colors.backgroundAlt ? theme.colors.backgroundAlt : theme.colors.background : theme.colors.background};
  }

  ${CardLink}:focus,${CardLink}:active,${CardLink}:hover & {
    border-color: ${({ theme }) => theme.colors.secondary};
    background-color: ${({ theme }) => theme.invertButtons ?
    theme.colors.backgroundAlt ? theme.colors.backgroundAlt : theme.colors.background : theme.colors.background};
  }
`;
// replaces .cardwarn
const CardContentWarning = styled.p`
  color: ${({ theme }) => theme.colors.secondary};
  
`;
// replaces .contentIcon
const CardContentTitleIcon = styled(Image)`
  object-fit: "contain";
  margin-right: 8px;
  aspect-ratio: 1;
  height: 28px;
`;
// replaces .contentTitle
const CardContentTitleWrap = styled.div`
  position: relative;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-bottom: 1rem;

  h2 {
    margin: 0;
    white-space: nowrap;
  }
`;
const CardContentTitle = ({ content }: { content: Service }) => {
  return (
    <CardContentTitleWrap>
      {
        content.icon ? (
          <CardContentTitleIcon alt="icon" src={content.icon} width="28" height="28" sizes='10vw' />
        ) : (<></>)
      }
      <h2>{content.name}</h2>
    </CardContentTitleWrap>
  )
}
// Card Content Component for Services Page
const MobileServiceCardOld = ({ content }: { content: Service }) => {
  let ret;
  if (content.href) {
    ret = (
      <CardLink href={content.href}>
        <PageCard>
          <CardContentTitle content={content} />
          <p>{content.desc}</p>
          <CardContentWarning>{content.warn}</CardContentWarning>
        </PageCard>
        <OnlineStatus status={content.status}>{content.status}</OnlineStatus>
      </CardLink>
    )
  }
  else {
    ret = (
      <CardStyleWrap>
        <PageCard>
          <CardContentTitle content={content} />
          <p>{content.desc}</p>
          <CardContentWarning>{content.warn}</CardContentWarning>
        </PageCard>
        <OnlineStatus status={content.status}>{content.status}</OnlineStatus>
      </CardStyleWrap>
    )
  }
  return ret;
}