import * as React from 'react';
import styled from 'styled-components';
import {
  rem,
  zIndex
} from '../../style/function.style';

import { PostCardConfig, AllDetail } from '../type';
import {
  FlexRowBox,
  FlexItem,
  PostErrorTip,
  skeletonOneLine,
  skeletonTwoLine
} from '../index.style';
import { formatDescription } from './utils';
import { LofterPostTags } from './Tags';
import { LofterPostLike } from './Like';

export const Container = styled.div`
  display: block;
  box-sizing: border-box;
  width: 100%;
  padding: ${rem(32)} 0;
  border-bottom: ${rem(1)} solid #ededed;
  position: relative;
  .nw-dark-mode &{
    border-color: #333;
  }
  &:last-of-type{
    border-bottom: none;
  }
`;

export const TitleRowBox = styled.div`
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: baseline;
`;
export const Title = styled.div<{ skeleton?: boolean }>`
  flex: 1 1 auto;
  width: 100%;
  height: ${rem(40)};
  font-size: ${rem(28)};
  line-height: ${rem(40)};
  color: #2e2e2e;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  ${({ skeleton }) => (
    skeleton ? `
      width: 60%;
      ${skeletonOneLine}
    ` : ''
  )}
  .nw-dark-mode &{
    color: #ccc;
  }
`;
export const Description = styled.div<{ skeleton?: boolean }>`
  height: ${rem(62)};
  margin-top: ${rem(16)};
  margin-bottom: ${rem(26)};
  font-size: ${rem(24)};
  line-height: ${rem(32)};
  color: #b3b3b3;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  ${({ skeleton }) => (
    skeleton ? skeletonTwoLine : ''
  )}
  .nw-dark-mode &{
    color: #888;
  }
`;
export const AuthorName = styled.div<{ skeleton?: boolean }>`
  flex: 0 0 auto;
  font-size: ${rem(24)};
  line-height: ${rem(40)};
  color: #909090;
  white-space: nowrap;
  ${({ skeleton }) => (
    skeleton ? `
      width: 30%;
      ${skeletonOneLine}
    ` : ''
  )}
`;

export const MoreRowBox = styled.div`
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
`;
export class LofterPostTextRowCard extends React.Component<{
  postCardConfig: PostCardConfig,
  post?: false | AllDetail,
}> {
  render() {
    const {
      post,
      postCardConfig
    } = this.props;
    const {
      title,
      description
    } = postCardConfig;
    const skeleton = !post;

    return (
      <Container
        onClick={() => {
          if (post) {
            window.location.href = post.postPageUrl;
          }
        }}
      >
        <TitleRowBox>
          <Title
            skeleton={(!title) && skeleton}
          >{title || (post && post.title)}</Title>
          <AuthorName
            skeleton={skeleton}
          >{post && post.blogInfo?.blogNickName}</AuthorName>
        </TitleRowBox>
        <Description
          skeleton={(!description) && skeleton}
          dangerouslySetInnerHTML={{
            __html: formatDescription(description || (post && post.digest))
          }}
        />
        <MoreRowBox>
          <FlexItem
            flex={1}
          >
            <LofterPostTags
              tagList={post && post.tagList || []}
            />
          </FlexItem>
          <FlexItem
            flex={0}
          >
            {
              post && (
                <LofterPostLike
                  post={post}
                />
              )
            }
          </FlexItem>
        </MoreRowBox>
        {
          post === false && (
            <PostErrorTip>文章状态异常</PostErrorTip>
          )
        }
        {
          (!postCardConfig.postId) && (
            <PostErrorTip>未指定文章/合集/推集</PostErrorTip>
          )
        }
      </Container>
    )
  }
}
