import { useCallback, useState } from "react";
import { observer } from "mobx-react-lite";
import { useRouter } from "next/router";
// icons
import { PlusIcon } from "lucide-react";
// types
import { IIssueDisplayFilterOptions, IIssueDisplayProperties, IIssueFilterOptions } from "@plane/types";
// ui
import { Breadcrumbs, Button, LayersIcon } from "@plane/ui";
// components
import { BreadcrumbLink } from "@/components/common";
import { DisplayFiltersSelection, FiltersDropdown, FilterSelection } from "@/components/issues";
import { CreateUpdateWorkspaceViewModal } from "@/components/workspace";
// constants
import {
  DP_APPLIED,
  DP_REMOVED,
  FILTER_APPLIED,
  FILTER_REMOVED,
  FILTER_SEARCHED,
  LP_UPDATED,
} from "@/constants/event-tracker";
import { EIssueFilterType, EIssuesStoreType, ISSUE_DISPLAY_FILTERS_BY_LAYOUT } from "@/constants/issue";
import { EUserWorkspaceRoles } from "@/constants/workspace";
import { useLabel, useMember, useUser, useIssues, useEventTracker } from "@/hooks/store";

export const GlobalIssuesHeader: React.FC = observer(() => {
  // states
  const [createViewModal, setCreateViewModal] = useState(false);
  // router
  const router = useRouter();
  const { workspaceSlug, globalViewId } = router.query;
  // store hooks
  const {
    issuesFilter: { filters, updateFilters },
  } = useIssues(EIssuesStoreType.GLOBAL);
  const {
    membership: { currentWorkspaceRole },
  } = useUser();
  const { workspaceLabels } = useLabel();
  const {
    workspace: { workspaceMemberIds },
  } = useMember();
  const { captureIssuesFilterEvent, captureIssuesDisplayFilterEvent } = useEventTracker();

  const issueFilters = globalViewId ? filters[globalViewId.toString()] : undefined;

  const handleFiltersUpdate = useCallback(
    (key: keyof IIssueFilterOptions, value: string | string[]) => {
      if (!workspaceSlug || !globalViewId) return;
      const newValues = Array.from(issueFilters?.filters?.[key] ?? []);

      if (Array.isArray(value)) {
        // this validation is majorly for the filter start_date, target_date custom
        value.forEach((val) => {
          if (!newValues.includes(val)) newValues.push(val);
          else newValues.splice(newValues.indexOf(val), 1);
        });
      } else {
        if (issueFilters?.filters?.[key]?.includes(value)) newValues.splice(newValues.indexOf(value), 1);
        else newValues.push(value);
      }

      const event = (issueFilters?.filters?.[key] ?? []).length > newValues.length ? FILTER_REMOVED : FILTER_APPLIED;
      updateFilters(
        workspaceSlug.toString(),
        undefined,
        EIssueFilterType.FILTERS,
        { [key]: newValues },
        globalViewId.toString()
      ).then(() => {
        captureIssuesFilterEvent({
          eventName: event,
          payload: {
            routePath: router.asPath,
            filters: issueFilters,
            filter_property: value,
            filter_type: key,
          },
        });
      });
    },
    [workspaceSlug, issueFilters, updateFilters, globalViewId, captureIssuesFilterEvent, router.asPath]
  );

  const handleDisplayFilters = useCallback(
    (updatedDisplayFilter: Partial<IIssueDisplayFilterOptions>) => {
      if (!workspaceSlug || !globalViewId) return;
      updateFilters(
        workspaceSlug.toString(),
        undefined,
        EIssueFilterType.DISPLAY_FILTERS,
        updatedDisplayFilter,
        globalViewId.toString()
      ).then(() =>
        captureIssuesDisplayFilterEvent({
          eventName: LP_UPDATED,
          payload: {
            property_type: Object.keys(updatedDisplayFilter).join(","),
            property: Object.values(updatedDisplayFilter)?.[0],
            routePath: router.asPath,
            filters: issueFilters,
          },
        })
      );
    },
    [workspaceSlug, updateFilters, globalViewId, issueFilters, captureIssuesDisplayFilterEvent, router.asPath]
  );

  const handleDisplayProperties = useCallback(
    (property: Partial<IIssueDisplayProperties>) => {
      if (!workspaceSlug || !globalViewId) return;
      updateFilters(
        workspaceSlug.toString(),
        undefined,
        EIssueFilterType.DISPLAY_PROPERTIES,
        property,
        globalViewId.toString()
      ).then(() =>
        captureIssuesDisplayFilterEvent({
          eventName: Object.values(property)?.[0] === true ? DP_APPLIED : DP_REMOVED,
          payload: {
            display_property: Object.keys(property).join(","),
            routePath: router.asPath,
            filters: issueFilters,
          },
        })
      );
    },
    [workspaceSlug, updateFilters, globalViewId, issueFilters, captureIssuesDisplayFilterEvent, router.asPath]
  );

  const isAuthorizedUser = !!currentWorkspaceRole && currentWorkspaceRole >= EUserWorkspaceRoles.MEMBER;

  return (
    <>
      <CreateUpdateWorkspaceViewModal isOpen={createViewModal} onClose={() => setCreateViewModal(false)} />
      <div className="relative z-[15] flex h-[3.75rem] w-full items-center justify-between gap-x-2 gap-y-4 bg-custom-sidebar-background-100 p-4">
        <div className="relative flex gap-2">
          <Breadcrumbs>
            <Breadcrumbs.BreadcrumbItem
              type="text"
              link={
                <BreadcrumbLink label={`All Issues`} icon={<LayersIcon className="h-4 w-4 text-custom-text-300" />} />
              }
            />
          </Breadcrumbs>
        </div>
        <div className="flex items-center gap-2">
          <>
            <FiltersDropdown title="Filters" placement="bottom-end">
              <FilterSelection
                layoutDisplayFiltersOptions={ISSUE_DISPLAY_FILTERS_BY_LAYOUT.my_issues.spreadsheet}
                filters={issueFilters?.filters ?? {}}
                handleFiltersUpdate={handleFiltersUpdate}
                labels={workspaceLabels ?? undefined}
                memberIds={workspaceMemberIds ?? undefined}
                onSearchCapture={() =>
                  captureIssuesFilterEvent({
                    eventName: FILTER_SEARCHED,
                    payload: {
                      routePath: router.asPath,
                      current_filters: issueFilters?.filters,
                      layout: issueFilters?.displayFilters?.layout,
                    },
                  })
                }
              />
            </FiltersDropdown>
            <FiltersDropdown title="Display" placement="bottom-end">
              <DisplayFiltersSelection
                layoutDisplayFiltersOptions={ISSUE_DISPLAY_FILTERS_BY_LAYOUT.my_issues.spreadsheet}
                displayFilters={issueFilters?.displayFilters ?? {}}
                handleDisplayFiltersUpdate={handleDisplayFilters}
                displayProperties={issueFilters?.displayProperties ?? {}}
                handleDisplayPropertiesUpdate={handleDisplayProperties}
              />
            </FiltersDropdown>
          </>
          {isAuthorizedUser && (
            <Button variant="primary" size="sm" prependIcon={<PlusIcon />} onClick={() => setCreateViewModal(true)}>
              New View
            </Button>
          )}
        </div>
      </div>
    </>
  );
});
