import React, { useEffect, useState } from "react";
import { Box, Modal, Typography, Divider, Grid, Chip, Button } from "@mui/material";
import CloseIcon from "@mui/icons-material/Close";
import { useSelector } from "react-redux";
import { RootState } from "../../../redux/reducers";
import { CustomIconButton } from "../../AddUserModal/styled.css";

type Props = {
	open: boolean;
	setViewModal: Function;
	handleView: (id: number | undefined) => void;
	id?: number | undefined;
};

const style = {
	position: "absolute",
	top: "50%",
	left: "50%",
	transform: "translate(-50%, -50%)",
	width: 500,
	bgcolor: "#000000",
	borderRadius: 2,
	boxShadow: "0px 4px 20px rgba(0, 0, 0, 0.15)",
	p: 0,
	display: "flex",
	flexDirection: "column",
	alignItems: "center",
};

export default function StationViewModal({ open, setViewModal, id }: Props) {
	const { stations } = useSelector(
		(state: RootState) => state.stationReducer
	);
	const [selectedStation, setSelectedStation] = useState<any>(null);

	useEffect(() => {
		if (id) {
			const station = stations.find((station) => station.id === id);
			setSelectedStation(station || null);
		}
	}, [id, stations]);

	return (
		<Modal
			open={open}
			aria-labelledby="modal-title"
			aria-describedby="modal-description"
		>
			<Box sx={style}>
				<Box
					sx={{
						display: "flex",
						justifyContent: "space-between",
						alignItems: "center",
						backgroundColor: "#000000",
						color: "#D0E1E9",
						padding: "20px 24px",
						borderRadius: "10px 10px 0 0",
					}}
				>
					<Typography
						id="modal-title"
						variant="h5"
						fontWeight="bold"
						sx={{ width: "100%" }}
					>
						<Box
							sx={{
								display: "flex",
								alignItems: "center",
								justifyContent: "space-between",
								width: "100%",
							}}
						>
							<Box
								sx={{ flex: 1, textAlign: "center" }}
								color="#D0E1E9"
							>
								{selectedStation?.name || "Station"}'s Details
							</Box>
							<CustomIconButton
								onClick={() => setViewModal(false)}
								aria-label="Close"
								sx={{ marginLeft: "55px" }}
							>
								<CloseIcon />
							</CustomIconButton>
						</Box>
					</Typography>
				</Box>
				{/* <Divider sx={{ width: "100%" }} /> */}
				<Box
					sx={{
						backgroundColor: "#b5c4cb",
						padding: "20px",
						borderRadius: "0 0 10px 10px",
					}}
				>
					{selectedStation ? (
						<Grid container spacing={2} sx={{ width: "100%" }}>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Station Name:</strong>{" "}
									<Typography variant="body2" color="#454545">
										{selectedStation.name}
									</Typography>
								</Typography>
							</Grid>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Station Location:</strong>{" "}
									<Typography variant="body2" color="#454545">
										{selectedStation.registeredAddress}
									</Typography>
								</Typography>
							</Grid>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Total Slots:</strong>
									<Typography variant="body2" color="#454545">
										{selectedStation.totalSlots}
									</Typography>
								</Typography>
							</Grid>
							<Grid item xs={6}>
								<Typography variant="body1" color="#000000">
									<strong>Status:</strong>
									<Typography variant="body2" color="#454545">
										{selectedStation.status === 1
											? "Available"
											: "Not Available"}
									</Typography>
								</Typography>
							</Grid>

							{/* Display Vehicles */}
							<Grid item xs={12}>
								<Typography variant="body1" color="#000000">
									<strong>Vehicles:</strong>
								</Typography>
								<Box
									sx={{
										display: "flex",
										flexWrap: "wrap",
										gap: 1,
										marginTop: 1,
									}}
									color="#454545"
								>
									{selectedStation.allowedCars &&
									selectedStation.allowedCars.length > 0 ? (
										selectedStation.allowedCars.map(
											(car: any, index: number) => (
												<Typography
													key={car.id || index}
													variant="body2"
													sx={{
														color: "#454545",
														fontWeight: 500,
													}}
												>
													{car.name}
													{","}
												</Typography>
											)
										)
									) : (
										<Typography
											variant="body2"
											color="#454545"
										>
											No vehicles available
										</Typography>
									)}
								</Box>
							</Grid>
						</Grid>
					) : (
						<Typography align="center">
							No station found with this ID
						</Typography>
					)}
					<Box
						sx={{
							display: "flex",
							justifyContent: "flex-end",
							mt: 3,
						}}
					>
						<Button
							onClick={() => setViewModal(false)}
							sx={{
								backgroundColor: "#000000",
								color: "#D0E1E9",
								borderRadius: "8px",
								fontSize: "16px",
								width: "117px",
								"&:hover": { backgroundColor: "#454545" },
							}}
						>
							Close
						</Button>
					</Box>
				</Box>
			</Box>
		</Modal>
	);
}
