import { DataGrid, GridColDef, GridRenderCellParams } from "@mui/x-data-grid";
import { ruRU } from "@mui/x-data-grid/locales";
import { authStore, carrierStore, languageStore, vehicleStore, SearchInput, selectedCityStore, cityStore } from "@shared";
import { useEffect, useState, useMemo } from "react";
import { observer } from "mobx-react-lite";
import { Eye, Pencil, Trash2, Minus } from "lucide-react";
import { useNavigate } from "react-router-dom";
import { CreateButton, DeleteModal } from "@widgets";
import { VEHICLE_TYPES } from "@shared";
import { Box, CircularProgress } from "@mui/material";

export const VehicleListPage = observer(() => {
  const { vehicles, getVehicles, deleteVehicle } = vehicleStore;
  const { getCarriers } = carrierStore;
  const navigate = useNavigate();
  const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
  const [isBulkDeleteModalOpen, setIsBulkDeleteModalOpen] = useState(false);
  const [rowId, setRowId] = useState<number | null>(null);
  const [ids, setIds] = useState<number[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [paginationModel, setPaginationModel] = useState({
    page: 0,
    pageSize: 50,
  });
  const { language } = languageStore;
  const canWriteVehicles = authStore.canWrite("devices");
  const [searchQuery, setSearchQuery] = useState("");

  useEffect(() => {
    const fetchData = async () => {
      setIsLoading(true);
      await getVehicles();
      await getCarriers(language);
      await cityStore.getCities("ru");
      setIsLoading(false);
    };
    fetchData();
  }, [language, selectedCityStore.cityVersion]);

  const columns: GridColDef[] = [
    {
      field: "tail_number",
      headerName: "Бортовой номер",
      flex: 1,
      renderCell: (params: GridRenderCellParams) => {
        return (
          <div className="w-full h-full flex items-center">
            {params.value ? (
              params.value
            ) : (
              <Minus size={20} className="text-red-500" />
            )}
          </div>
        );
      },
    },
    {
      field: "type",
      headerName: "Тип",
      flex: 1,
      renderCell: (params: GridRenderCellParams) => {
        return (
          <div className="w-full h-full flex items-center">
            {params.value ? (
              VEHICLE_TYPES.find((type) => type.value === params.row.type)
                ?.label || params.row.type
            ) : (
              <Minus size={20} className="text-red-500" />
            )}
          </div>
        );
      },
    },
    {
      field: "carrier",
      headerName: "Перевозчик",
      flex: 1,
      renderCell: (params: GridRenderCellParams) => {
        return (
          <div className="w-full h-full flex items-center">
            {params.value ? (
              params.value
            ) : (
              <Minus size={20} className="text-red-500" />
            )}
          </div>
        );
      },
    },
    {
      field: "city",
      headerName: "Город",
      flex: 1,
      renderCell: (params: GridRenderCellParams) => {
        return (
          <div className="w-full h-full flex items-center">
            {params.value ? (
              params.value
            ) : (
              <Minus size={20} className="text-red-500" />
            )}
          </div>
        );
      },
    },
    {
      field: "actions",
      headerName: "Действия",
      width: 200,
      align: "center" as const,
      headerAlign: "center" as const,
      sortable: false,
      renderCell: (params: GridRenderCellParams) => {
        const canWrite = authStore.canWrite("devices");
        return (
          <div className="flex h-full gap-7 justify-center items-center">
            {canWrite && (
              <button title="Редактировать" onClick={() => navigate(`/vehicle/${params.row.id}/edit`)}>
                <Pencil size={20} className="text-blue-500" />
              </button>
            )}
            <button title="Просмотр" onClick={() => navigate(`/vehicle/${params.row.id}`)}>
              <Eye size={20} className="text-green-500" />
            </button>
            {canWrite && (
              <button
                title="Удалить"
                onClick={() => {
                  setIsDeleteModalOpen(true);
                  setRowId(params.row.id);
                }}
              >
                <Trash2 size={20} className="text-red-500" />
              </button>
            )}
          </div>
        );
      },
    },
  ];

  const { selectedCityId } = selectedCityStore;

  const rows = useMemo(() => {
    if (!selectedCityId) return [];
    const query = searchQuery.trim().toLowerCase();
    return (vehicles.data ?? [])
      .filter((vehicle) => vehicle.vehicle.city_id === selectedCityId)
      .filter(
        (vehicle) =>
          !query ||
          (vehicle.vehicle.tail_number ?? "").toLowerCase().includes(query) ||
          (vehicle.vehicle.carrier ?? "").toLowerCase().includes(query)
      )
      .map((vehicle) => ({
        id: vehicle.vehicle.id,
        tail_number: vehicle.vehicle.tail_number,
        type: vehicle.vehicle.type,
        carrier: vehicle.vehicle.carrier,
        city: cityStore.cities.ru.data.find((c) => c.id === vehicle.vehicle.city_id)?.name,
      }));
  }, [vehicles.data, selectedCityId, searchQuery]);

  return (
    <>
      <div style={{ width: "100%" }}>
        <div className="flex justify-between items-center mb-10">
          <h1 className="text-2xl">Транспортные средства</h1>

          <CreateButton
            label="Создать транспортное средство"
            path="/vehicle/create"
          />
        </div>

        {rows.length > 0 && (
          <SearchInput value={searchQuery} onChange={setSearchQuery} />
        )}

        {canWriteVehicles && ids.length > 0 && (
          <div className="flex justify-end mb-5 duration-300">
            <button
              className="px-4 py-2 bg-red-500 text-white rounded flex gap-2 items-center"
              onClick={() => setIsBulkDeleteModalOpen(true)}
            >
              <Trash2 size={20} className="text-white" /> Удалить выбранные (
              {ids.length})
            </button>
          </div>
        )}

        <DataGrid
          rows={rows}
          columns={columns}
          checkboxSelection={canWriteVehicles}
          disableRowSelectionExcludeModel
          disableRowSelectionOnClick
          loading={isLoading}
          paginationModel={paginationModel}
          onPaginationModelChange={setPaginationModel}
          pageSizeOptions={[50]}
          onRowSelectionModelChange={
            canWriteVehicles
              ? (newSelection: any) => {
                  if (Array.isArray(newSelection)) {
                    const selectedIds = newSelection.map(
                      (id: string | number) => Number(id)
                    );
                    setIds(selectedIds);
                  } else if (
                    newSelection &&
                    typeof newSelection === "object" &&
                    "ids" in newSelection
                  ) {
                    const idsSet = newSelection.ids as Set<string | number>;
                    const selectedIds = Array.from(idsSet).map(
                      (id: string | number) => Number(id)
                    );
                    setIds(selectedIds);
                  } else {
                    setIds([]);
                  }
                }
              : undefined
          }
          localeText={ruRU.components.MuiDataGrid.defaultProps.localeText}
          slots={{
            noRowsOverlay: () => (
              <Box sx={{ mt: 5, textAlign: "center", color: "text.secondary" }}>
                {isLoading ? (
                  <CircularProgress size={20} />
                ) : !selectedCityId ? (
                  "Выберите город"
                ) : (
                  "Нет транспортных средств"
                )}
              </Box>
            ),
          }}
        />
      </div>

      <DeleteModal
        open={isDeleteModalOpen}
        onDelete={async () => {
          if (rowId) {
            await deleteVehicle(rowId);
          }
          setIsDeleteModalOpen(false);
          setRowId(null);
        }}
        onCancel={() => {
          setIsDeleteModalOpen(false);
          setRowId(null);
        }}
      />

      <DeleteModal
        open={isBulkDeleteModalOpen}
        onDelete={async () => {
          await Promise.all(ids.map((id) => deleteVehicle(id)));
          getVehicles();
          setIsBulkDeleteModalOpen(false);
          setIds([]);
        }}
        onCancel={() => {
          setIsBulkDeleteModalOpen(false);
        }}
      />
    </>
  );
});
