import { Meteor } from 'meteor/meteor';
import React, { useState, useEffect } from 'react';
import { useTracker } from 'meteor/react-meteor-data';
import _ from 'lodash';
import {Link, useSearchParams} from "react-router-dom";
import Tabs from '@mui/material/Tabs';
import Tab from '@mui/material/Tab';
import Box from '@mui/material/Box';

const RenderUsage = ({data}) => {
	return (
		<>
			<ul>
				{data.map((next, i) => (
					<li key={next._id}>
						{next.person && (
							<>
								User: {next.person.firstName} {next.person.lastName} {next.person.grade ? "~ " + next.person.grade : ""} (<Link to={"/search?email=" + encodeURIComponent(next.email)}>{next.email}</Link>)<br/>
							</>
						)}
						{/*Device ID: <Link to={"/search?deviceId=" + encodeURIComponent(next.deviceId)}>{next.deviceId}</Link><br/>*/}
						{next.assetType && (
							<>Asset Type: {next.assetType.name}<br/></>
						)}
						Serial: <Link to={"/search?serial=" + encodeURIComponent(next.serial)}>{next.serial}</Link><br/>
						{next.asset && (
							<>Asset ID: <Link to={"/search?assetId=" + encodeURIComponent(next.asset.assetId)}>{next.asset.assetId}</Link><br/></>
						)}
						{new Date(next.startTime).toLocaleDateString("en-US") + "-" + new Date(next.endTime).toLocaleDateString("en-US")}<br/>
						{next.assignedTo && (
							<>Currently assigned to: {next.assignedTo.firstName} {next.assignedTo.lastName} {next.assignedTo.grade ? "~ " + next.assignedTo.grade : ""} ({next.assignedTo.email})</>
						)}
					</li>
				))}
			</ul>
		</>
	)
}

const RenderAssignments = ({data}) => {
	return (
		<>
			<ul>
				{data.map((next, i) => (
					<li key={next._id + "/" + next.assetId}>
						{next.assignee && (
							<>
								User: {next.assignee.firstName} {next.assignee.lastName} {next.assignee.grade ? "~ " + next.assignee.grade : ""} (<Link to={"/search?email=" + encodeURIComponent(next.assignee.email)}>{next.assignee.email}</Link>)<br/>
							</>
						)}
						Serial: <Link to={"/search?serial=" + encodeURIComponent(next.serial)}>{next.serial}</Link><br/>
						{next.asset && (
							<>
								Asset ID: <Link to={"/search?assetId=" + encodeURIComponent(next.asset.assetId)}>{next.asset.assetId}</Link><br/>
							</>
						)}
						{next.assetType && (
							<>
								Asset Type: {next.assetType.name}<br/>
							</>
						)}
						{new Date(next.startDate).toLocaleDateString("en-US") + (next.endDate ? "-" + new Date(next.endDate).toLocaleDateString("en-US") : " - Still Assigned")}<br/>
						{next.comment && (
							<>Comment: {next.comment}<br/></>
						)}
						Start Condition: {next.startCondition}<br/>
						{next.startConditionDetails && <>Details: {next.startConditionDetails}<br/></>}
						{next.endDate && (
							<>
								End Condition: {next.endCondition}<br/>
								{next.endConditionDetails && <>Details: {next.endConditionDetails}<br/></>}
							</>
						)}
					</li>
				))}
			</ul>
		</>
	)
}

export default () => {
	// const query = queryString.parse(search)
	const [usageData, setUsageData] = useState([])
	const [assignmentData, setAssignmentData] = useState([])
	const [search, setSearch] = useSearchParams()
	
	useEffect(() => {
		let args;
		
		if(search.get('studentId')) {
			args = {studentId: search.get('studentId')}
		}
		else if(search.get('staffId')) {
			args = {staffId: search.get('staffId')}
		}
		else if(search.get('email')) {
			args = {email: search.get('email')}
		}
		else if(search.get('deviceId')) {
			args = {deviceId: search.get('deviceId')}
		}
		else if(search.get('serial')) {
			args = {serial: search.get('serial')}
		}
		else if(search.get('assetId')) {
			args = {assetId: search.get('assetId')}
		}

		Meteor.call('DataCollection.chromebookData', args, (err, result) => {
			if (err) console.error(err)
			else setUsageData(result)
		})
		
		// if(search.get('studentId')) {
		// 	args = {studentId: search.get('studentId')}
		// }
		// else if(search.get('staffId')) {
		// 	args = {staffId: search.get('staffId')}
		// }
		// else if(search.get('serial')) {
		// 	args = {serial: search.get('serial')}
		// }
		// else if(search.get('assetId')) {
		// 	args = {assetId: search.get('assetId')}
		// }

		Meteor.call('AssetAssignmentHistory.get', args, (err, result) => {
			if (err) console.error(err)
			else setAssignmentData(result)
		})
	}, [search])
	
	const [tabIndex, setTabIndex] = useState(0)
	
	console.log(assignmentData)

	// return (search.get('resultType') === 'usage' ? <RenderUsage data={data}/> : <RenderAssignments data={data}/>)
	return (
		<>
			<Box sx={{width: '100%'}}>
				<Tabs value={tabIndex} onChange={(e, index) => {setTabIndex(index)}} aria-label='nav tabs'>
					<Tab label="Usage"/>
					<Tab label="Assignments"/>
				</Tabs>
			</Box>
			<div role="tabpanel" hidden={tabIndex !== 0}>
				<RenderUsage data={usageData}/>
			</div>
			<div role="tabpanel" hidden={tabIndex !== 1}>
				<RenderAssignments data={assignmentData}/>
			</div>
		</>
	)
}