import { useRouter } from "next/router";
import { useEffect } from "react";

export default function useWebsocketConsumer(
  ws,
  setUser,
  user,
  setData,
  dataRoom
) {
  const router = useRouter();
  console.log("New instance of", setData, dataRoom);

  useEffect(() => {
    if (ws != null) {
      ws.onmessage = (e) => {
        console.log("eeeee", e);
        let data = JSON.parse(e.data);
        let type = data.type;

        switch (type) {
          case "roomJoined": {
            let id_code = data.id_code;
            setUser({ clientId: data.clientId, user: data.identity });
            sessionStorage.setItem("clientId", data.clientId);
            router.push({ pathname: `/room/${id_code}` }, undefined, {
              shallow: true,
            });
            break;
          }
          case "accept_room": {
            let code = data.code;
            ws.send(
              JSON.stringify({
                data: { type: "logAcceptedWaiter", code: code },
              })
            );
            break;
          }
          case "waitingRoom": {
            let nick = data.nick;
            let id_code = data.id_code;
            sessionStorage.setItem("waiter", true);
            sessionStorage.setItem("waiter_nick", nick);
            router.push({ pathname: "/room/waiting/" + id_code }, undefined, {
              shallow: true,
            });
            break;
          }
          case "reloged": {
            let id_code = data.id_code;
            setUser({ clientId: data.clientId, user: data.identity });
            sessionStorage.setItem("clientId", data.clientId);
            router.push({ pathname: `/room/${id_code}` }, undefined, {
              shallow: true,
            });
            break;
          }
          case "reconnectError": {
            router.push({ pathname: "/room/join" });
            break;
          }
          case "reconnected": {
            setUser({ clientId: data.clientId, user: data.identity });
            break;
          }
          case "joined": {
            if (dataRoom) {
              let nick = data.nick;
              let owner = data.owner;
              let online = data.online;
              let code = data.code;

              let oldParticipants = dataRoom.room.participants;
              let oldWaiters = dataRoom.room.waiters;
              setData({
                room: {
                  ...dataRoom.room,
                  participants: [
                    ...oldParticipants,
                    { nick: nick, owner: owner, online: online, code: code },
                  ],
                  waiters: [...oldWaiters.filter((w) => w.code != code)],
                },
              });
            }
            break;
          }
          case "disconnect_participant": {
            if (dataRoom) {
              let oldParticipants = dataRoom.room.participants;
              let nick = data.nick;
              console.log(oldParticipants, nick);
              let participant = oldParticipants.filter(
                (p) => p.nick == nick
              )[0];
              console.log("part", participant);
              participant.online = false;
              setData({
                room: {
                  ...dataRoom.room,
                  participants: [
                    ...oldParticipants.filter((p) => p.nick != nick),
                    participant,
                  ],
                },
              });
            }
            break;
          }
          case "reconnect_participant": {
            if (dataRoom) {
              let oldParticipants = dataRoom.room.participants;
              let nick = data.nick;
              console.log(oldParticipants, nick);
              let participant = oldParticipants.filter(
                (p) => p.nick == nick
              )[0];
              console.log("part", participant);
              participant.online = true;
              setData({
                room: {
                  ...dataRoom.room,
                  participants: [
                    ...oldParticipants.filter((p) => p.nick != nick),
                    participant,
                  ],
                },
              });
            }
            break;
          }
          case "add_waiter": {
            console.log("Herre new waiteeerr ", setData, dataRoom);
            if (dataRoom) {
              let nick = data.nick;
              let code = data.code;
              let chanName = data.chanName;

              let oldWaiters = dataRoom.room.waiters;
              setData({
                room: {
                  ...dataRoom.room,
                  waiters: [
                    ...oldWaiters,
                    { nick: nick, code: code, chanName: chanName },
                  ],
                },
              });
            }
            break;
          }
          case "del_waiter": {
            if (dataRoom) {
              let code = data.code;

              let oldWaiters = dataRoom.room.waiters;
              setData({
                room: {
                  ...dataRoom.room,
                  waiters: [...oldWaiters.filter((w) => w.code != code)],
                },
              });
            }
            break;
          }
          case "banned": {
            if (dataRoom) {
              let nick = data.nick;
              let code = data.code;

              if (user.user.code != code) {
                let oldParticipants = dataRoom.room.participants;
                let oldParcours = dataRoom.room.parcours;
                setData({
                  room: {
                    ...dataRoom.room,
                    participants: [
                      ...oldParticipants.filter((p) => p.nick != nick),
                    ],
                    parcours: [
                      ...oldParcours.map((p) => {
                        return {
                          ...p,
                          challenger: [...p.challenger((c) => c.nick != nick)],
                        };
                      }),
                    ],
                  },
                });
              } else if (user.user.code == code) {
                router.push({ pathname: "/room/join" });
              }
            }
            break;
          }

          case "add_parcours": {
            if (dataRoom) {
              let new_parcours = data.parcours;
              let id_code = dataRoom.room.id_code;
              let oldParcours = dataRoom.room.parcours;
              setData({
                room: {
                  ...dataRoom.room,
                  parcours: [...oldParcours, new_parcours],
                },
              });
               router.push(
                {
                  pathname: `/room/${id_code}`,
                  query: { action: "view" },
                },
                undefined,
                {
                  shallow: true,
                }
              ); 
            }
            break
          }

          case "challenge_parcours":
            {
              if (dataRoom) {
                return
              }
              break
            }
          default:
            console.log("Default", type);
        }
      };
    }
  }, [ws, dataRoom]);
}
