import { Pressable, View } from "react-native"
import { SearchKeywordHighlight } from "../../../ui-components/search-keyword-highlight/search-keyword-highlight.component"
import { useStyles } from "./search-messages.style"
import { MessageType, RoomKindEnum } from "@openchat/types"
import {
  useAccountId,
  useChannel,
  useCommunity,
  useGroup,
  useMember,
  useRoom,
  useUserLastSeenAs,
  useUserUnified,
} from "@openchat/ui"
import { RoomUtils, UserProfileUtils } from "@openchat/models"
import { StyledText } from "../../../ui-components/styled-text/styled-text"
import { Icon } from "../../../ui-components/icon/icon.component"
import { UserPhoto } from "../../user/user-photo/user-photo.component"
import { ChannelIcon } from "../../channel/channel-icon/channel-icon.component"
import { NavigationRef } from "../../../navigation/navigation.hooks"
import { StackActions } from "@react-navigation/native"

export function SearchMessages({
  message,
  keyword,
}: {
  message: MessageType
  keyword: string
}) {
  // ---------------------------------------------------------------------------
  // variables
  // ---------------------------------------------------------------------------
  console.log("====================message================")
  console.log(message)
  console.log("====================end ================")
  const styles = useStyles()
  const { groupId, communityId, channelId, userId } = RoomUtils.extractIds(
    message.roomId,
  )

  const room = useRoom(message.roomId)
  const accountId = useAccountId()
  const group = useGroup(groupId)
  const channel = useChannel(channelId)
  const userLastSeen = useUserLastSeenAs(message.senderId)
  const user = useUserUnified(userLastSeen)
  const searchMember = useMember(message.senderMemberId)
  const searchUser = useUserUnified(
    UserProfileUtils.toPartialFromMember(searchMember),
  )
  const community = useCommunity(communityId)

  const isCurrentUserSender =
    (message.senderId && user?.id === accountId) ||
    (message.senderMemberId && searchUser?.id === accountId)

  const displayUser = message.senderId ? user : searchUser

  const isDirectMessage = !community && !channel && !!userId

  const directMessageText = isDirectMessage
    ? isCurrentUserSender
      ? "you in direct message with"
      : "in direct message with you"
    : ""

  let typeMessage =
    room &&
    (room.kind === RoomKindEnum.DM ||
      room.kind === RoomKindEnum.CHANNEL_REGULAR)
  let typeQuestion = room && room.kind === RoomKindEnum.QUESTION
  let typeIssue = room && room.kind === RoomKindEnum.ISSUE
  let typeNews = room && room.kind === RoomKindEnum.NEWS
  let typeComments = room && room.kind === RoomKindEnum.BOARD_COMMENTS
  let typeReply = room && room.kind === RoomKindEnum.CHANNEL_REGULAR_REPLY

  // ---------------------------------------------------------------------------
  return (
    <View style={styles.wrapper}>
      <View style={styles.messagesName}>
        {community && channel && (
          <View style={{ flexDirection: "row" }}>
            <SearchKeywordHighlight
              text={community.name}
              style={styles.communityName}
              keyword={keyword}
            />
            <View
              style={{ flexDirection: "row", alignItems: "center", gap: 5 }}
            >
              <View style={styles.border} />
              <ChannelIcon
                size="1.4"
                isPrivate={channel.isPrivate}
                type={channel.type}
              />
            </View>
            <View style={{ flexDirection: "row", marginLeft: 5 }}>
              <StyledText style={styles.titleRoleName}>
                {channel.name}
              </StyledText>
            </View>
          </View>
        )}

        <View style={styles.userMessagesName}>
          {isCurrentUserSender ? (
            <>
              {displayUser && (
                <View
                  style={{
                    flexDirection: "row",
                    gap: 10,
                    alignItems: "center",
                  }}
                >
                  <StyledText style={styles.msgText}>
                    message by {directMessageText}
                  </StyledText>

                  <UserPhoto size={25} user={displayUser} />
                  <StyledText
                    style={styles.nameUser}
                    ellipsizeMode="tail"
                    numberOfLines={1}
                  >
                    <SearchKeywordHighlight
                      text={displayUser.name}
                      keyword={keyword}
                    />
                  </StyledText>
                </View>
              )}
            </>
          ) : (
            <View
              style={{ flexDirection: "row", gap: 10, alignItems: "center" }}
            >
              {displayUser && (
                <>
                  <StyledText style={styles.msgText}>message by</StyledText>
                  <UserPhoto size={25} user={displayUser} />
                  <StyledText
                    style={styles.nameUser}
                    ellipsizeMode="tail"
                    numberOfLines={1}
                  >
                    <SearchKeywordHighlight
                      text={displayUser.name}
                      keyword={keyword}
                    />
                  </StyledText>
                  <StyledText style={styles.msgText}>
                    {directMessageText}
                  </StyledText>
                </>
              )}
            </View>
          )}
        </View>
      </View>
      <Pressable
        style={styles.messagesWrap}
        onPress={() => {
          NavigationRef.dispatch(
            StackActions.push("MessageRoom", { roomId: room && room.id }),
          )
        }}
      >
        <SearchKeywordHighlight
          text={message.text}
          keyword={keyword}
          style={styles.messages}
        />
      </Pressable>
    </View>
  )
}
please fix this   let typeMessage =
    room &&
    (room.kind === RoomKindEnum.DM ||
      room.kind === RoomKindEnum.CHANNEL_REGULAR)
  let typeQuestion = room && room.kind === RoomKindEnum.QUESTION
  let typeIssue = room && room.kind === RoomKindEnum.ISSUE
  let typeNews = room && room.kind === RoomKindEnum.NEWS
  let typeComments = room && room.kind === RoomKindEnum.BOARD_COMMENTS
  let typeReply = room && room.kind === RoomKindEnum.CHANNEL_REGULAR_REPLY part