import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { useEffect } from 'react';
import {
  fetchWorkThunk,
  workSelectionSlice,
} from '../../../states/patient/worklist/slices/workSlice';
import { Table } from 'antd';
import { FormattedMessage } from 'react-intl';
import { RootState, AppDispatch } from '../../../states/store';
import { Task } from '@/domain/work';

const columns = [
  {
    title: (
      <FormattedMessage
        id="worklistTable.StudyInstanceUID"
        defaultMessage="worklistTable.StudyInstanceUID"
      />
    ),
    dataIndex: 'StudyInstanceUID',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.StudyID"
        defaultMessage="worklistTable.StudyID"
      />
    ),
    dataIndex: 'StudyID',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.SpecificCharacterSet"
        defaultMessage="worklistTable.SpecificCharacterSet"
      />
    ),
    dataIndex: 'SpecificCharacterSet',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.AccessionNumber"
        defaultMessage="worklistTable.AccessionNumber"
      />
    ),
    dataIndex: 'AccessionNumber',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PatientID"
        defaultMessage="worklistTable.PatientID"
      />
    ),
    dataIndex: 'PatientID',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PatientName"
        defaultMessage="worklistTable.PatientName"
      />
    ),
    dataIndex: 'PatientName',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.DisplayPatientName"
        defaultMessage="worklistTable.DisplayPatientName"
      />
    ),
    dataIndex: 'DisplayPatientName',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PatientSize"
        defaultMessage="worklistTable.PatientSize"
      />
    ),
    dataIndex: 'PatientSize',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PatientAge"
        defaultMessage="worklistTable.PatientAge"
      />
    ),
    dataIndex: 'PatientAge',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PatientSex"
        defaultMessage="worklistTable.PatientSex"
      />
    ),
    dataIndex: 'PatientSex',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.AdmittingTime"
        defaultMessage="worklistTable.AdmittingTime"
      />
    ),
    dataIndex: 'AdmittingTime',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.RegSource"
        defaultMessage="worklistTable.RegSource"
      />
    ),
    dataIndex: 'RegSource',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.StudyStatus"
        defaultMessage="worklistTable.StudyStatus"
      />
    ),
    dataIndex: 'StudyStatus',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.RequestedProcedureID"
        defaultMessage="worklistTable.RequestedProcedureID"
      />
    ),
    dataIndex: 'RequestedProcedureID',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PerformedProtocolCodeValue"
        defaultMessage="worklistTable.PerformedProtocolCodeValue"
      />
    ),
    dataIndex: 'PerformedProtocolCodeValue',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PerformedProtocolCodeMeaning"
        defaultMessage="worklistTable.PerformedProtocolCodeMeaning"
      />
    ),
    dataIndex: 'PerformedProtocolCodeMeaning',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PerformedProcedureStepID"
        defaultMessage="worklistTable.PerformedProcedureStepID"
      />
    ),
    dataIndex: 'PerformedProcedureStepID',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.StudyDescription"
        defaultMessage="worklistTable.StudyDescription"
      />
    ),
    dataIndex: 'StudyDescription',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.StudyStartDatetime"
        defaultMessage="worklistTable.StudyStartDatetime"
      />
    ),
    dataIndex: 'StudyStartDatetime',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.ScheduledProcedureStepStartDate"
        defaultMessage="worklistTable.ScheduledProcedureStepStartDate"
      />
    ),
    dataIndex: 'ScheduledProcedureStepStartDate',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.StudyLock"
        defaultMessage="worklistTable.StudyLock"
      />
    ),
    dataIndex: 'StudyLock',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.OperatorID"
        defaultMessage="worklistTable.OperatorID"
      />
    ),
    dataIndex: 'OperatorID',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.Modality"
        defaultMessage="worklistTable.Modality"
      />
    ),
    dataIndex: 'Modality',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.Views"
        defaultMessage="worklistTable.Views"
      />
    ),
    dataIndex: 'Views',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.Thickness"
        defaultMessage="worklistTable.Thickness"
      />
    ),
    dataIndex: 'Thickness',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.PatientType"
        defaultMessage="worklistTable.PatientType"
      />
    ),
    dataIndex: 'PatientType',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.StudyType"
        defaultMessage="worklistTable.StudyType"
      />
    ),
    dataIndex: 'StudyType',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.QRCode"
        defaultMessage="worklistTable.QRCode"
      />
    ),
    dataIndex: 'QRCode',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.IsExported"
        defaultMessage="worklistTable.IsExported"
      />
    ),
    dataIndex: 'IsExported',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.IsEdited"
        defaultMessage="worklistTable.IsEdited"
      />
    ),
    dataIndex: 'IsEdited',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.WorkRef"
        defaultMessage="worklistTable.WorkRef"
      />
    ),
    dataIndex: 'WorkRef',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.IsAppended"
        defaultMessage="worklistTable.IsAppended"
      />
    ),
    dataIndex: 'IsAppended',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.CreationTime"
        defaultMessage="worklistTable.CreationTime"
      />
    ),
    dataIndex: 'CreationTime',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.MappedStatus"
        defaultMessage="worklistTable.MappedStatus"
      />
    ),
    dataIndex: 'MappedStatus',
  },
  {
    title: (
      <FormattedMessage
        id="worklistTable.IsDelete"
        defaultMessage="worklistTable.IsDelete"
      />
    ),
    dataIndex: 'IsDelete',
  },
];

const WorklistTable: React.FC = () => {
  const dispatch: AppDispatch = useDispatch();
  const worklistData = useSelector(
    (state: RootState) => state.workEntities.data
  );
  const filters = useSelector((state: RootState) => state.search);
  const page = useSelector((state: RootState) => state.workPagination.page);
  const pageSize = useSelector(
    (state: RootState) => state.workPagination.pageSize
  );
  const selectedIds = useSelector(
    (state: RootState) => state.workSelection.selectedIds
  );

  useEffect(() => {
    dispatch(fetchWorkThunk({ page, pageSize, filters }));
  }, [dispatch, filters, page, pageSize]);

  const handleRowClick = (record: Task) => {
    console.log('Row clicked:', JSON.stringify(record, null, 2));
    console.log('Selected IDs before:', record.StudyInstanceUID);
    dispatch(
      workSelectionSlice.actions.setSelectedIds([record.StudyInstanceUID])
    );
  };

  return (
    <Table
      columns={columns}
      dataSource={worklistData}
      rowKey="StudyInstanceUID"
      onRow={(record) => ({
        onClick: () => handleRowClick(record),
      })}
      rowHoverable={false}
      rowClassName={(record) =>
        selectedIds.includes(record.StudyInstanceUID)
          ? 'bg-yellow-500 hover:bg-yellow-800'
          : ''
      }
    />
  );
};

export default WorklistTable;
