import useAxios from "axios-hooks";
import React from "react";

const useRooms = () => {
  //GET List Rooms Personal
  const [{ ...getPersonalRoomsData }, getPersonalRooms] = useAxios(
    {
      method: "GET",
    },
    {
      manual: true,
    }
  );

  const handleGetPersonalRooms = async () => {
    try {
      await getPersonalRooms({
        url: `/notify/v1/rooms/`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //GET List My Rooms Pinned personal
  const [{ ...getPinnedRoomsPersonalData }, getPinnedRoomsPersonal] = useAxios(
    {
      method: "GET",
    },
    {
      manual: true,
    }
  );

  const handleGetPinnedRoomsPersonal = async (limit = 50) => {
    try {
      await getPinnedRoomsPersonal({
        url: `/notify/v1/rooms/?limit=${limit}&pinned=true`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //PATCH Change Pin Status Personal
  const [{ ...changePinStatusPersonalData }, changePinStatusPersonal] =
    useAxios(
      {
        method: "PATCH",
      },
      {
        manual: true,
      }
    );
  const handleChangePinStatusPersonal = async (room_id, type) => {
    try {
      await changePinStatusPersonal({
        url: `/notify/v1/rooms/${room_id}/change_pin_status/`,
        // data: {
        //   ...data,
        // },
        params: {
          type,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  //PATCH Change Pin Status
  const [{ ...changePinStatusData }, changePinStatus] = useAxios(
    {
      method: "PATCH",
    },
    {
      manual: true,
    }
  );
  const handleChangePinStatus = async (account_type, room_id, type) => {
    try {
      await changePinStatus({
        url: `/notify/v1/${account_type}/rooms/${room_id}/change_pin_status/`,
        // data: {
        //   ...data,
        // },
        params: {
          type,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  //PATCH Change Pin Status Instructor affiliate
  const [{ ...changePinStatusAffiliateData }, changePinStatusAffiliate] =
    useAxios(
      {
        method: "PATCH",
      },
      {
        manual: true,
      }
    );
  const handleChangePinStatusAffiliate = async (
    enterprise_account_id,
    room_id,
    type
  ) => {
    try {
      await changePinStatusAffiliate({
        url: `/notify/v1/instructor/${enterprise_account_id}/rooms/${room_id}/change_pin_status/`,
        // data: {
        //   ...data,
        // },
        params: {
          type,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  //GET Retrieve Room
  const [{ ...RetrieveRoomData }, retrieveRoom] = useAxios(
    {
      method: "GET",
    },
    {
      manual: true,
    }
  );
  const handleRetrieveRoom = async (id) => {
    try {
      await retrieveRoom({
        url: `/notify/v1/rooms/${id}/`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //POST Create Public Room
  const [{ ...createPublicRoomData }, createPublicRoom] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleCreatePublicRoom = async (data) => {
    try {
      await createPublicRoom({
        url: `/notify/v1/rooms/create_public/`,
        data: {
          ...data,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  //POST Create Private Room
  const [{ ...createPrivateRoomData }, createPrivateRoom] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleCreatePrivateRoom = async (data) => {
    try {
      await createPrivateRoom({
        url: `/notify/v1/rooms/create_private/`,
        data: {
          ...data,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  //PUT Join Group
  const [{ ...joinGroupData }, joinGroup] = useAxios(
    {
      method: "PUT",
    },
    {
      manual: true,
    }
  );

  const handleJoinGroup = async () => {
    try {
      await joinGroup({
        url: `/notify/v1/rooms/create_public/`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //PUT Leave Group
  const [{ ...leaveGroupData }, leaveGroup] = useAxios(
    {
      method: "PUT",
    },
    {
      manual: true,
    }
  );

  const handleLeaveGroup = async (id) => {
    try {
      await leaveGroup({
        url: `/notify/v1/rooms/${id}/leave_group/`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //GET List Rooms
  const [{ ...getRoomsData }, getRooms] = useAxios(
    {
      method: "GET",
    },
    {
      manual: true,
    }
  );

  const handleGetRooms = async (accountType, limit = 50) => {
    try {
      await getRooms({
        url: `/notify/v1/${accountType}/rooms/?limit=${limit}&pinned=false`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //GET List Rooms Affiliate
  const [{ ...getRoomsAffiliateData }, getRoomsAffiliate] = useAxios(
    {
      method: "GET",
    },
    {
      manual: true,
    }
  );

  const handleGetRoomsAffiliate = async (enterprise_account_id, limit = 50) => {
    try {
      await getRoomsAffiliate({
        url: `/notify/v1/instructor/${enterprise_account_id}/rooms/?limit=${limit}&pinned=false`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //GET List Pinned Rooms
  const [{ ...getAllPinnedRoomsData }, getAllPinnedRooms] = useAxios(
    {
      method: "GET",
    },
    {
      manual: true,
    }
  );

  const handleGetAllPinnedRooms = async (accountType, limit = 50) => {
    try {
      await getAllPinnedRooms({
        url: `/notify/v1/${accountType}/rooms/?limit=${limit}&pinned=true`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //GET List Pinned Rooms Affiliate
  const [{ ...getAllPinnedRoomsAffiliateData }, getAllPinnedRoomsAffiliate] =
    useAxios(
      {
        method: "GET",
      },
      {
        manual: true,
      }
    );

  const handleGetAllPinnedRoomsAffiliate = async (
    enterprise_account_id,
    limit = 50
  ) => {
    try {
      await getAllPinnedRoomsAffiliate({
        url: `/notify/v1/instructor/${enterprise_account_id}/rooms/?limit=${limit}&pinned=true`,
      });
    } catch (err) {
      console.log(err);
    }
  };

  //POST create room based on course
  const [{ ...createRoomData }, createRoom] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleCreateRoom = async (
    account_type,
    account_id,
    course_id,
    data,
    enterprise_account_id
  ) => {
    try {
      let apiUrl;

      if (account_type === "instructor") {
        apiUrl = enterprise_account_id
          ? `/notify/v1/instructor/${enterprise_account_id}/rooms/chat_course/` // Affiliate instructor
          : `/notify/v1/${account_type}/rooms/chat_course/`; // Non-affiliate instructor
      } else if (account_type === "enterprise") {
        apiUrl = `/notify/v1/${account_type}/rooms/chat_course/`; // Enterprise
      } else if (account_type === "personal") {
        apiUrl = `/notify/v1/rooms/chat_course/`; // For personal account type
      } else {
        console.log("Invalid account type");
      }
      await createRoom({
        url: apiUrl,
        data: {
          ...data,
        },
        params: {
          account_id,
          course_id,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  //POST create room based on course Affiliate
  const [{ ...createRoomAffiliateData }, createRoomAffiliate] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleCreateRoomAffiliate = async (
    enterprise_account_id,
    affiliate_account_id,
    enterprise_course_id,
    data
  ) => {
    try {
      await createRoomAffiliate({
        url: `/notify/v1/instructor/${enterprise_account_id}/rooms/chat_course/`,
        data: {
          ...data,
        },
        params: {
          affiliate_account_id,
          enterprise_course_id,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  //POST create room based on course personal
  const [{ ...createRoomPersonalData }, createRoomPersonal] = useAxios(
    {
      method: "POST",
    },
    {
      manual: true,
    }
  );

  const handleCreateRoomPersonal = async (account_id, course_id, data) => {
    try {
      await createRoomPersonal({
        url: `/notify/v1/rooms/chat_course/`,
        data: {
          ...data,
        },
        params: {
          account_id,
          course_id,
        },
      });
    } catch (err) {
      console.log(err);
    }
  };

  return {
    RetrieveRoomData,
    handleRetrieveRoom,
    createPublicRoomData,
    handleCreatePublicRoom,
    createPrivateRoomData,
    handleCreatePrivateRoom,
    joinGroupData,
    handleJoinGroup,
    leaveGroupData,
    handleLeaveGroup,
    changePinStatusData,
    handleChangePinStatus,
    getRoomsData,
    handleGetRooms,
    getPersonalRoomsData,
    handleGetPersonalRooms,
    getAllPinnedRoomsData,
    handleGetAllPinnedRooms,
    createRoomData,
    handleCreateRoom,
    changePinStatusAffiliateData,
    handleChangePinStatusAffiliate,
    getRoomsAffiliateData,
    handleGetRoomsAffiliate,
    getAllPinnedRoomsAffiliateData,
    handleGetAllPinnedRoomsAffiliate,
    getAllPinnedRoomsData,
    handleGetPinnedRoomsPersonal,
    getPinnedRoomsPersonalData,
    handleGetPinnedRoomsPersonal,
    changePinStatusPersonalData,
    handleChangePinStatusPersonal,
    createRoomPersonalData,
    handleCreateRoomPersonal,
    createRoomAffiliateData,
    handleCreateRoomAffiliate,
  };
};

export default useRooms;
