import React, { useEffect, useState } from "react";
import styled from "styled-components";
import { Button ,Spin } from "antd";
import { truncateCommitId } from '../common/util';
import { getImageUrl } from 'educoder';
import Files from '../Merge/Files';
import Tree from "./img/tree.png";
import User from "../Component/User";
import axios from "axios";
import { Link } from "react-router-dom";

const Infos = styled.div`
  border: 1px solid #FAFCFF;
  margin-bottom:15px;
  & .commitinfos {
    background-color: #f1f8ff;
    border: 1px solid rgba(42, 97, 255, 0.23);
    border-radius: 3px 3px 0px 0px;
    padding: 10px 20px 10px 16px;
    & .f-wrap-between{
      align-items: center;
    }
    & .task-hide{
      width: 65rem;
      overflow:hidden;
      white-space:normal;
      word-break:break-all;
      font-weight: bold;
      color: #333333;
      font-size: 16px;
    }
  }
  & > .f-wrap-between {
    padding: 14px 20px 14px 16px;
    border-radius: 3px 3px 0px 0px;
    border: 1px solid #D0D0D0;
    .df{
      align-items: center;
      & .underline:hover{
        text-decoration: underline;
      }
    }
  }
`;
//提交详情页
export default (props) => {
  const {match , history } = props;
  const [data, setData] = useState({undefined});
  const [commit, setCommit] = useState(undefined);
  const [parents, setParents] = useState(undefined);
  const [committer, setCommitter] = useState(undefined);
  const [isSpin, setIsSpin] = useState(true);
  const { sha , projectsId, owner, branchName } = match.params;
  useEffect(() => {
    if (projectsId && owner && sha) {
      const url = `/${owner}/${projectsId}/commits/${sha}.json`;
      axios
        .get(url)
        .then(result => {
          if (result) {
            setData(result.data);
            setCommit(result.data.commit);
            setParents(result.data.parents);
            setCommitter(result.data.committer || (result.data.commit && result.data.commit.committer));
            setIsSpin(false);
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }, [projectsId , owner, sha]);

  function timeFormat(seconds) {
    const time = new Date(seconds*1000);
    return time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate()+"  "+(time.getHours()+1)+":"+time.getMinutes();
  }
  
  return (
    <div className="main" style={{padding:"0px",border:"none"}}>
      <Spin spinning={isSpin}>
        <Infos>
          <div className="commitinfos">
            <div className="f-wrap-between">
              <div>
                {commit && commit.message &&
                  <pre className="task-hide">{commit.message}</pre>
                }
                <Link to={`/${owner}/${projectsId}/tree/${branchName}`}><i className="iconfont icon-fenzhi2 font-18"></i>{branchName}</Link>
              </div>
              <Button type="primary" onClick={()=>{history.push(`/${owner}/${projectsId}/tree/${truncateCommitId(sha)}`)}} className="btnblue" style={{height:"36px"}}>浏览文件</Button>
            </div>
          </div>
          <div className="f-wrap-between" style={{ alignItems: "center" }}>
            <ul className="df">
              <User
                id = {committer && committer.id}
                url={(committer && getImageUrl(`/${committer.image_url}`))|| "https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3025493530,1989042357&fm=26&gp=0.jpg"}
                name={committer && committer.name}
                login={committer && committer.login}
              />
              {commit && commit.timestamp && <li className="ml4">提交于{timeFormat(commit.timestamp)}</li>}
            </ul>
            <li className="df">
              {
                parents && parents.length > 0 && parents.map((item,key)=>{
                  return(
                    <div className="ml40 f-wrap-alignCenter">
                      <label className="mr8">父节点</label>
                      <img src={Tree} alt="sha" width={"16px"} className="mr4"/>
                      <Link to={`/${owner}/${projectsId}/commits/${truncateCommitId(`${item.sha}`)}/${branchName}`} className="underline">{truncateCommitId(item.sha)}</Link>
                    </div>
                  )
                })
              }
              <div className="ml40 f-wrap-alignCenter">
                <label className="mr8">当前节点</label>
                <img src={Tree} alt="sha" width={"16px"} className="mr4"/>
                <span>{truncateCommitId(sha)}</span>
              </div>
            </li>
          </div>
        </Infos>
        <Files 
          history={history} 
          data={data} 
          owner={owner} 
          projectsId={projectsId} 
        />
      </Spin>
    </div>
  );
};
