import React, { useState, useEffect, useContext, memo } from 'react';
import { Progress, Input, Tooltip , Spin } from 'antd'
import { getUrl2, isDev, CBreadcrumb, ActionBtn, ThemeContext,formatSeconds} from 'educoder'
import axios from 'axios'
import moment from 'moment'
import playIcon from './images/play.png'
import ClipboardJS from 'clipboard'
import defaultImg from './images/default.png';
import './InfosVideo.css';
/**
cover_url: "http://video.educoder.net/f6ba49c3944b43ee98736898e31b7d88/snapshots/12da3f7df07c499b8f0fc6dc410094e9-00005.jpg"
created_at: "2019-08-12 13:48:26"
file_url: "http://video.educoder.net/sv/4c7eb4-16c845ee09c/4c7eb4-16c845ee09c.mp4"
id: 1
published_at: "2019-08-12 15:38:00"
title: "测试标题"
updated_at: "2019-08-12 17:17:09"
 */
let _clipboard = null;
const clipboardMap = {}
function VideoInReviewItem (props) {
    const theme = useContext(ThemeContext);
    const { history, file_url , play_url , cover_url , transcoded , title, created_at, published_at, isReview, id
        , onEditVideo, onMaskClick, getCopyText, showNotification,vv,play_duration,operation , deleteVideo , moveVideo ,link, people_num,total_time,Setmyktid} = props;
    useEffect(()=> {
        if (!isReview) {
            _clipboard = new ClipboardJS(`.copybtn_item_${id}`);
            _clipboard.on('success', (e) => {
                showNotification('复制成功')
            });
            clipboardMap[id] = _clipboard
        }
        return () => {
            if (clipboardMap[id]) {
                clipboardMap[id].destroy();
                clipboardMap[id] = null;
            }
        }
    }, [])



    const username = props.match.params.username
    function toList() {
        history.push(`/users/${username}/videos`)
    }
    function toUpload() {
        history.push(`/users/${username}/videos/upload`)
    }
    const imgUrl = (!transcoded && !link) ? defaultImg : (cover_url || defaultImg);
    return (
        <div className={`${isReview ? 'videoInReviewItem' : 'nItem'} videoItem`}>
            <Spin tip="正在转码，请稍等..." spinning={!transcoded && !link}>
                <img className="cover" src={imgUrl} alt=""></img>
                { link ?
                    <a href={link} target='_blank' className="otherLinkPanel">
                        <span className="otherLink">外链</span>
                    </a>
                    : ""
                }

                {!isReview && <div className="mask" onClick={() => onMaskClick(props)}></div>}
                {!isReview && !link && transcoded &&
                <div className="playWrap" onClick={() => onMaskClick(props)}>
                    <img className="play mp23" src={playIcon}></img>
                    {/* {play_duration===0?"":<div className={"play_duration"}>累计学习时长：{play_duration} h</div>} */}
                </div>
                }
            </Spin>
            <div className="square-main">
                <div className="title overflowHidden1"
                    title={title && title.length > 20 ? title : ''}
                >{title}</div>
                <div className="df buttonRow mb10">
                {/* <div className={"play_duration"}>累计学习时长：{play_duration} h</div> */}
                {/* <span className="time">{moment(published_at || created_at).format('YYYY-MM-DD HH:mm:ss')}{people_num}</span> */}

                {link||total_time===undefined?<span className="time"> </span>:<span className="time">累计学习时长：{
                    formatSeconds(total_time)}
                {/* total_time<60?total_time+' s':total_time/60<60?(total_time/60).toFixed(0)+' min':(total_time/3600).toFixed(1)+ ' h' */}
                </span>}
                </div>
                <div className="df buttonRow">
                    {/* 2019-09-01 10:00:22 */}
                    <span className={"dianjilianicon"}>
                         {total_time===undefined?'':!people_num || (people_num && people_num)===0 ? "" : <Tooltip title="观看人数" placement="bottom">
                             <i className={`icon-dianjiliang iconfont dianjilianicon font-12`}>   {!people_num || (people_num && people_num)===0?"":people_num}</i>
                         </Tooltip> }
                    </span>


                    <div>

                        {
                            isReview !== true && moveVideo &&
                            <Tooltip title="移动" placement="bottom">
                                <i className="icon-yidong iconfont font-15" onClick={() => moveVideo(props)}
                                   style={{    marginTop: '1px', display: 'inline-block'}}
                                ></i>
                            </Tooltip>
                        }


                        {
                            props&&props.mykt!=undefined? isReview !== true && !link ?
                            <Tooltip title="统计" placement="bottom">
                                <i className="icon-tongji1 iconfont font-15" onClick={() => Setmyktid(props.newcourse_video_id,props.newtitle)}
                                   style={{    marginTop: '1px', display: 'inline-block'}}
                                ></i>
                            </Tooltip>:"":""
                        }


                        {
                            isReview !== true && operation &&
                            <Tooltip title="编辑" placement="bottom">
                                <i className="icon-bianji1 iconfont" onClick={() => onEditVideo(props)}
                                style={{    marginTop: '1px', display: 'inline-block'}}
                                ></i>
                            </Tooltip>
                        }

                        {
                            isReview !== true && !link ?
                            <Tooltip title="复制链接" placement="bottom">
                                <i className={`icon-fuzhi iconfont copybtn_item_${id}`} data-clipboard-text={getCopyText((play_url || file_url), cover_url)}></i>
                            </Tooltip>:""
                        }

                        {
                            deleteVideo &&
                            <Tooltip title="删除" placement="bottom">
                                <i className="icon-shanchu iconfont" onClick={() => deleteVideo(props)}
                                   style={{    marginTop: '1px', display: 'inline-block'}}
                                ></i>
                            </Tooltip>
                        }
                    </div>
                </div>
            </div>
        </div>
    )
}

export default VideoInReviewItem
