import React from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import {Recording, RecordingFormat} from '../Common/Api';

type Props = {
    recording: Recording;
	format: RecordingFormat;
	isAdmin : boolean;
    deleteRecording: (recording: Recording) => void;
    storeRecording: (recording: Recording, format: RecordingFormat) => void;
	publishRecording: (recording: Recording, publish: boolean) => void;
}

const RecordingRow = ({recording, format, isAdmin, deleteRecording, storeRecording, publishRecording}: Props): JSX.Element => {

	function checkPublished(recording: Recording, onChange: (value: boolean) => void) {
		return (
			<div>
				<input id={'bbb-record-state-' + recording.id}
					type="checkbox"
					className="checkbox"
					checked={recording.state === 'published'}
					onChange={(event) =>  onChange(event.target.checked)} />
				<label htmlFor={'bbb-record-state-' + recording.id}>{t('bbb', 'Published')}</label>
			</div>
		);
	}

	return (
		<tr key={recording.id}>
			<td className="start icon-col">
				<a href={format.url} className="action-item" target="_blank" rel="noopener noreferrer" title={t('bbb', 'Open recording')}>
					<span className="icon icon-external icon-visible"></span>
				</a>
			</td>
			<td className="share icon-col">
				<CopyToClipboard text={format.url} options={{format:'text/plain'}}>
					<button className="action-item copy-to-clipboard" title={t('bbb', 'Copy to clipboard')}>
						<span className="icon icon-clippy icon-visible" ></span>
					</button>
				</CopyToClipboard>
			</td>
			<td className="icon-col">
				<button className="action-item" onClick={() => storeRecording(recording, format)} title={t('bbb', 'Save as file')}>
					<span className="icon icon-add-shortcut icon-visible"></span>
				</button>
			</td>
			<td>
				{(new Date(recording.startTime)).toLocaleString()}
			</td>
			<td>
				{format.length === 0 ? '< 1 min' : (format.length + ' min')}
			</td>
			<td>
				{n('bbb', '%n participant', '%n participants', recording.participants)}
			</td>
			<td>
				{format.type}
			</td>
			<td>
				{isAdmin && checkPublished(recording, (checked) => {
					publishRecording(recording, checked);
				})}
			</td>
			<td className="remove icon-col">
				{isAdmin &&
					<button className="action-item" onClick={() => deleteRecording(recording)} title={t('bbb', 'Delete')}>
						<span className="icon icon-delete icon-visible"></span>
					</button>
				}
			</td>
		</tr>
	);
};

export default RecordingRow;
