import { DataGridColumnHeader, DataGridProvider } from '@/components';
import { Toaster } from '@/components/ui/sonner';
import { apiConfig } from '@/config/api.config';
import { useCallApi } from '@/hooks';
import { ColumnDef } from '@tanstack/react-table';
import { createContext, useCallback, useEffect, useMemo, useState } from 'react';
import ListToolbar from '../blocks/ListToolBar';

interface SelectedUser {
  id: string;
  name: string;
  code: string;
  prefix: string;
  trailer: string;
  format: string;
  grouping_separator: string;
  decimal_separator: string;
}

interface CurrencyProps {
  id: string;
  name: string;
  code: string;
  prefix: string;
  trailer: string;
  format: string;
  grouping_separator: string;
  decimal_separator: string;
}

interface ContextProps {
  showEditDialog: boolean;
  handleEditDialog: (show: boolean, selected_user: string | null) => void;
  showAddDialog: boolean;
  handleAddDialog: (show: boolean) => void;
  selectedUser: string | null;
  currencies: CurrencyProps[];
}

const initialProps: ContextProps = {
  showEditDialog: false,
  handleEditDialog: () => {},
  showAddDialog: false,
  handleAddDialog: () => {},
  selectedUser: null,
  currencies: []
};

const ManageCurrencyContext = createContext<ContextProps>(initialProps);
const API_URL = apiConfig.service_dashboard;

const ManageCurrencyContextProvider = ({ children }: { children: React.ReactNode }) => {
  const [showEditDialog, setShowEditDialog] = useState(false);
  const [showAddDialog, setShowAddDialog] = useState(false);
  const [selectedUser, setSelectedUser] = useState<string | null>(null);
  const [currencies, setCurrencies] = useState<CurrencyProps[]>([]);
  const { GetData } = useCallApi();

  useEffect(() => {
    const dummyData: CurrencyProps[] = [
      {
        id: '1',
        name: 'US Dollar',
        code: 'USD',
        prefix: '$',
        trailer: '',
        format: '#,##0.00',
        grouping_separator: ',',
        decimal_separator: '.'
      },
      {
        id: '2',
        name: 'Euro',
        code: 'EUR',
        prefix: '€',
        trailer: '',
        format: '#.##0,00',
        grouping_separator: '.',
        decimal_separator: ','
      },
      {
        id: '3',
        name: 'Japanese Yen',
        code: 'JPY',
        prefix: '¥',
        trailer: '',
        format: '#,##0',
        grouping_separator: ',',
        decimal_separator: '.'
      },
      {
        id: '4',
        name: 'Indonesia Rupiah',
        code: 'IDR',
        prefix: 'Rp',
        trailer: '',
        format: '#,##0',
        grouping_separator: '.',
        decimal_separator: ','
      }
    ];
    setCurrencies(dummyData);
  }, []);

  const handleEditDialog = useCallback((show: boolean, selected_user: string | null) => {
    setSelectedUser(show ? selected_user : null);
    setShowEditDialog(show);
  }, []);

  const handleAddDialog = useCallback((show: boolean) => {
    setShowAddDialog(show);
  }, []);

  const columns = useMemo<ColumnDef<any>[]>(
    () => [
      {
        accessorFn: (row) => row.id,
        id: 'id',
        header: ({ column }) => <DataGridColumnHeader title="ID" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[100px]'
        }
      },
      {
        accessorFn: (row) => row.name,
        id: 'name',
        header: ({ column }) => <DataGridColumnHeader title="Name" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.code,
        id: 'code',
        header: ({ column }) => <DataGridColumnHeader title="Code" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.prefix,
        id: 'prefix',
        header: ({ column }) => <DataGridColumnHeader title="Prefix" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.trailer,
        id: 'trailer',
        header: ({ column }) => <DataGridColumnHeader title="Trailer" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.format,
        id: 'format',
        header: ({ column }) => <DataGridColumnHeader title="Format" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.grouping_separator,
        id: 'grouping_separator',
        header: ({ column }) => <DataGridColumnHeader title="Grouping Separator" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        accessorFn: (row) => row.decimal_separator,
        id: 'decimal_separator',
        header: ({ column }) => <DataGridColumnHeader title="Decimal Separator" column={column} />,
        enableSorting: true,
        enableHiding: false,
        meta: {
          headerClassName: 'w-[250px]'
        }
      },
      {
        id: 'actions',
        enableSorting: false,
        header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
        cell: ({ row }) => {
          return (
            <>
              <button
                className="btn btn-sm btn-primary mr-2"
                onClick={() => handleEditDialog(true, row.original.id)}
              >
                Edit
              </button>
            </>
          );
        },
        meta: {
          headerClassName: 'w-[100px]',
          cellClassName: 'text-center'
        }
      }
    ],
    [handleAddDialog, handleEditDialog]
  );
  console.log(currencies);
  return (
    <div>
      <ManageCurrencyContext.Provider
        value={{
          showEditDialog,
          handleEditDialog,
          showAddDialog,
          handleAddDialog,
          selectedUser,
          currencies
        }}
      >
        <Toaster expand visibleToasts={9} duration={3000} />

        {currencies.length > 0 ? (
          <DataGridProvider
            columns={columns}
            data={currencies}
            pagination={{ size: 10 }}
            layout={{ card: true }}
            toolbar={<ListToolbar />}
            sorting={[{ id: 'id', desc: true }]}
            serverSide={true}
          >
            {children}
          </DataGridProvider>
        ) : (
          <p>loading</p>
        )}
      </ManageCurrencyContext.Provider>
    </div>
  );
};

export { ManageCurrencyContext, ManageCurrencyContextProvider };
export type { SelectedUser };
