import React, { useEffect, useState } from 'react';
import { Skeleton , Tooltip} from 'antd';
import { Link } from 'react-router-dom';
import { numFormat } from 'educoder';
import QuitBox from './quit';
import axios from 'axios';
import Quality from '../../Images/quality/quality.svg'


function DetailBanner({ history,list , owner , projectsId ,showNotification , url , pathname , state , urlFlag , projectDetail , platform ,open_devops ,current_user }){
  const [ menuName , setMenuName ] = useState(undefined);
  const [ visible , setVisible ] =  useState(false);
  useEffect(()=>{
    if(list){
      // 没有资源库banner但是通过连接进资源库页面时
      let a = list.filter(item=>item.menu_name === "resources");
      if((pathname && pathname==="source") && (a && a.length === 0)){
        history.push(`/${owner}/${projectsId}`);
      }
      let b = list.filter(item=>item.menu_name === "server");
      if((pathname && pathname.indexOf("server") > 0) && (b && b.length === 0)){
        history.push(`/${owner}/${projectsId}`);
      }
      setMenuName(list);
    }
  },[list]);

  function onSuccess(){
    const url = `/${owner}/${projectsId}/quit.json`;
    axios.post(url).then(result=>{
      if(result){
        showNotification("已成功退出仓库!");
        history.push(`/${current_user && current_user.login}`);
      }
    }).catch(error=>{})
  } 
  return(
    <div className="f-wrap-between mt25">
      <QuitBox visible={visible} onCancel={()=>setVisible(false)} name={projectDetail && projectDetail.name} onSuccess={onSuccess}/>
      {
        menuName && menuName.length> 0 && projectDetail ? 
        <ul className="headerMenu-wrapper">
          {
           Array.isArray(menuName)&& menuName.map((item,key)=>{
              return(
                <React.Fragment key={item.menu_name}>
                  {
                    item.menu_name === "home" &&
                    <li className={pathname==="about" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/about`, state }}>
                        <i className={"iconfont icon-zhuye-fill color-grey-3 mr5 font-14"}></i>
                        <span>主页</span>
                      </Link>
                    </li>
                  }
                  {
                    item.menu_name === "code" &&
                    <li className={(pathname==="" || urlFlag) ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}`, state }}>
                        <i className={"iconfont icon-daimakuicon1 color-grey-3 mr5 font-14"}></i>
                        <span>代码库</span>
                      </Link>
                    </li>
                  }
                  {
                    item.menu_name === "issues" &&
                    <li className={pathname==="issues" ? "active" : ""}>
                        <Link to={{ pathname: `/${owner}/${projectsId}/issues`, state }}>
                          <Tooltip placement="bottom">
                              <i className={"iconfont icon-yixiuicon1 color-grey-3 mr5 font-14"}></i>
                              <span>疑修(Issue)</span>
                          </Tooltip>
                          {projectDetail && projectDetail.issues_count ? <span className="num">{numFormat(projectDetail.issues_count)}</span> : ""}
                        </Link>
                    </li>
                  }
                  {
                    item.menu_name === "pulls" && projectDetail && parseInt(projectDetail.type) !== 2 && platform ?
                    <li className={pathname==="pulls" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/pulls`, state }}>
                        <i className={"iconfont icon-hebingqingqiu1 color-grey-3 mr5 font-14"}></i>
                        <span>合并请求(PR)</span>
                        {projectDetail && projectDetail.pull_requests_count ? <span className="num">{numFormat(projectDetail.pull_requests_count)}</span> : ""}
                      </Link>
                    </li>:""
                  }
                  {/* {
                    item.menu_name === "wiki" &&
                    <li className={pathname === "wiki" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/wiki`, state }}>
                        <i className={"iconfont icon-a-wikiicon1 color-grey-3 mr5 font-14"}></i>
                        <span>Wiki</span>
                      </Link>
                    </li>
                  } */}
                  {
                    item.menu_name === "devops" && platform ? 
                    <li className={pathname==="devops" ? "active" : ""}>
                      {/* <Link to={{ pathname: `/${owner}/${projectsId}/devops${open_devops ? `/dispose`:""}`, state }}> */}
                      <Link to={{ pathname: `/${owner}/${projectsId}/devops`, state:{...state,open_devops} }}>
                        <i className="iconfont icon-gongzuoliuicon font-13 mr5 color-grey-3"></i>引擎(Engine)
                        {projectDetail && projectDetail.ops_count ? <span>{projectDetail.ops_count}</span> : ""} 
                      </Link>
                    </li>
                    :""
                  }
                  {/* {
                    item.menu_name === "resources" &&
                    <li className={pathname==="source" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/source`, state }}>
                        <i className={pathname==="source" ? "iconfont icon-ziyuanpaihanghetuijian color-grey-3 mr5 font-14":"iconfont icon-ziyuanpaihanghetuijian color-grey-6 font-14 mr5"}></i>
                        <span>资源库</span>
                        {projectDetail && projectDetail.source_count ? <span className="num">{projectDetail.source_count}</span> :""}
                      </Link>
                    </li>
                  } */}
                  {
                    item.menu_name === "versions" &&
                    <li className={pathname==="milestones" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/milestones`, state }}> 
                        <i className={pathname==="milestones" ? "iconfont icon-lichengbeiicon color-grey-3 mr5 font-14":"iconfont icon-lichengbeiicon color-grey-6 font-14 mr5"}></i>
                        <span>里程碑</span>
                        {projectDetail && projectDetail.versions_count ? <span className="num">{numFormat(projectDetail.versions_count)}</span> :""}
                      </Link>
                    </li>
                  }
                  {/* {
                    item.menu_name === "wiki" &&
                    <li className={pathname === "wiki" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/wiki`, state }}>
                        <i className={"iconfont icon-a-wikiicon1 color-grey-3 mr5 font-14"}></i>
                        <span>维基(Wiki)</span>
                      </Link>
                    </li>
                  } */}
                  {
                    item.menu_name === "services" &&
                    <li className={pathname === "server" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/server`, state }}>
                        <i className={"iconfont icon-fuwuicon color-grey-3 mr5 font-15"} style={{marginTop:"1px"}}></i>
                        <span>服务</span>
                      </Link>
                    </li>
                  }
                  {
                    item.menu_name === "activity" &&
                     <li className={pathname==="activity" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/activity`, state }}>
                        <i className={pathname==="activity" ? "iconfont icon-dongtaiicon color-grey-3 mr5 font-14":"iconfont icon-dongtaiicon color-grey-6 font-14 mr5"}></i>
                        <span>动态</span>
                      </Link>
                    </li>
                  }
                  {
                    item.menu_name === "sonar" &&
                     <li className={pathname==="sonar" ? "active" : ""}>
                      <Link to={{ pathname: `/${owner}/${projectsId}/sonar`, state }}>
                        <img src={Quality} alt=''className='mt5 mr3' style={{width:'16px',height:'16px'}}/>
                        {/* <i className={pathname==="sonar" ? "iconfont icon-a-shujufenxi-liuliangfenxi3 color-grey-3 mr5 font-14":"iconfont icon-a-shujufenxi-liuliangfenxi3 color-grey-6 font-14 mr5"}></i> */}
                        <span>质量分析</span>
                      </Link>
                    </li>
                  }
                  {
                    item.menu_name === "settings" && platform &&
                    <li className={pathname === "settings" ? "active" : ""}>
                      <Link to={`/${owner}/${projectsId}/settings`}>
                        <i className={url && url.indexOf("/settings") > 0 ? "iconfont icon-cangkushezhiicon color-grey-3 mr5 font-14":"iconfont icon-cangkushezhiicon color-grey-6 font-14 mr5"}></i>
                        <span>仓库设置</span>
                      </Link>
                    </li>
                  }
                  {
                    item.menu_name === "quit" &&
                    <a onClick={()=>setVisible(true)}><i className="iconfont icon-tuichuicon color-grey-3 mr5 font-14"></i>退出仓库</a>
                  }
                </React.Fragment>
              )
            })
          }
        </ul>
        :
        <Skeleton paragraph={false} active={true}/>
      }
    </div>
  )
}
export default DetailBanner;