"use client";

import React from "react";
import Image from "next/image";
import moment from "moment";
import type { CustomView } from "./calendarTypes";
import type { useCalendar } from "./useCalendar";

const asset = (path: string) => `/assets/${path}`;

const viewButtons: Array<{ value: CustomView; en: string; ar: string }> = [
  { value: "day", en: "Day", ar: "يوم" },
  { value: "week", en: "Week", ar: "أسبوع" },
  { value: "month", en: "Month", ar: "شهر" },
  { value: "year", en: "Year", ar: "سنة" },
];

/**
 * The bar above the grid: the day/week/month/year switch, the arrows that step
 * by whichever unit is showing, "Today", and the search box.
 */
export default function CalendarToolbar({
  screen,
}: {
  screen: ReturnType<typeof useCalendar>;
}) {
  const {
    isRTL,
    view,
    setView,
    currentDate,
    setSearchQuery,
    handleNavigate,
    goToToday,
    timeRange,
  } = screen;

  // The arrows step by whatever the chosen range is.
  const navUnit =
    timeRange === "year"
      ? "year"
      : timeRange === "month"
        ? "month"
        : timeRange === "week"
          ? "week"
          : "day";

  return (
    <div className="gap-[10px] lg:flex md:flex flex-wrap justify-between items-center p-4 border-b bg-[#ececec] pr-[3%]">
      <div className="flex items-center mb-2 md:mb-0">
        <button
          onClick={() =>
            handleNavigate(
              moment(currentDate).subtract(1, navUnit).toDate()
            )
          }
          className="pl-2 pr-[1px] text-gray-600"
        >
          <Image
            src={asset(
              isRTL
                ? "calander/blackrightarrow.svg"
                : "calander/blackleftarrow.svg"
            )}
            alt="Previous"
            width={24}
            height={24}
          />
        </button>
        <button onClick={goToToday}>
          <Image
            src={asset("calander/today.svg")}
            alt="Today"
            width={60}
            height={24}
          />
        </button>
        <button
          onClick={() =>
            handleNavigate(
              moment(currentDate).add(1, navUnit).toDate()
            )
          }
          className="pl-[1px] text-gray-600"
        >
          <Image
            src={asset(
              isRTL
                ? "calander/blackleftarrow.svg"
                : "calander/blackrightarrow.svg"
            )}
            alt="Next"
            width={24}
            height={24}
          />
        </button>
      </div>

      <div className="flex space-x-1 mb-2 md:mb-0">
        {viewButtons.map((btn) => (
          <button
            key={btn.value}
            className={`px-3 py-1 text-sm rounded-md ${
              view === btn.value
                ? "bg-primary-5 text-white"
                : "text-[#71717A]"
            }`}
            onClick={() => setView(btn.value)}
          >
            {isRTL ? btn.ar : btn.en}
          </button>
        ))}
      </div>

      <div className="relative w-full md:w-auto">
        <input
          type="text"
          onChange={(event) => setSearchQuery(event.target.value)}
          placeholder={isRTL ? "بحث" : "Search"}
          className={`px-3 py-2 bg-[#E4E4E4] rounded-md text-sm w-full lg:w-48 md:w-48 ${
            isRTL ? "pr-8" : "pl-8"
          }`}
        />
        <Image
          className={`w-4 h-4 absolute top-1/2 transform -translate-y-1/2 text-gray-500 ${
            isRTL ? "right-2" : "left-2"
          }`}
          src={asset("calander/search.svg")}
          alt="Search"
          width={16}
          height={16}
        />
      </div>
    </div>
  );
}
