import { ScrollView, View } from "react-native";
import React, { useState, useEffect } from "react";
import { useTailwind } from "tailwind-rn";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import NoticeItem from "../components/NoticeItem";
import baseRequest from "../../../utils/baseRequest";
import { generateSignature } from "../../../utils/crypto";
import { Image } from "expo-image";
import Toast from "react-native-toast-message";
import Empty from "../../../components/Empty";
import systemIcon from "../../../assets/icon/others/m_system.png";
import payIcon from "../../../assets/icon/others/m_pay.png";
import activeIcon from "../../../assets/icon/others/m_active.png";
import examIcon from "../../../assets/icon/others/m_exam.png";
import { useDispatch } from "react-redux";
import { getNoticeCount } from "../../../store/reducer";
export default function SystemNotice({ navigation, route, ...props }) {
  const { type } = route.params;
  const tailwind = useTailwind();
  const insets = useSafeAreaInsets();
  const [data, setData] = useState([]);
  const dispatch = useDispatch();
  useEffect(() => {
    getData();
  }, []);
  const getData = async (searchValue) => {
    const apiUrl = process.env.EXPO_PUBLIC_API_URL;
    try {
      const base = await baseRequest();
      const body = {
        mid: base.b_mid,
        ...base,
      };
      const signature = await generateSignature(body);
      const _receiveResponse = await fetch(
        `${apiUrl}/api/notification/receive?signature=${signature}`,
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(body),
        }
      );
      const receive = await _receiveResponse.json();
      const _response = await fetch(
        `${apiUrl}/api/notification/list_by_mid?signature=${signature}`,
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({ n_type: type, ...body }),
        }
      );
      const _data = await _response.json();
      if (_data.ret === -1) {
        Toast.show({
          type: "error",
          text1: _data.msg,
          topOffset: 60,
        });
        return;
      }
      setData(_data.data.list.filter((it) => it.n_type == type));

      const _response2 = await fetch(
        `${apiUrl}/api/notification/read_all?signature=${signature}`,
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({ n_types: [type], ...body }),
        }
      );
      const _data2 = await _response2.json();
      if (_data2.ret === -1) {
        Toast.show({
          type: "error",
          text1: _data2.msg,
          topOffset: 60,
        });
        return;
      }
      dispatch(getNoticeCount(0));
    } catch (error) {
      console.error(error);
    }
  };
  return (
    <ScrollView>
      <View style={tailwind("p-4")}>
        {data.map((it) => (
          <NoticeItem
            key={it.id}
            data={it}
            navigation={navigation}
            hasLink={{ url: it.url, text: it.link_text }}
            leftIcon={
              <View>
                <Image
                  source={
                    it.n_type === 0
                      ? systemIcon
                      : it.n_type === 1
                      ? examIcon
                      : it.n_type === 2
                      ? payIcon
                      : activeIcon
                  }
                  contentFit="cover"
                  cachePolicy="disk"
                  transition={1000}
                  style={{ width: 32, height: 32 }}
                />
              </View>
            }
          />
        ))}
        {!data.length && <Empty type="nodata" />}
      </View>
    </ScrollView>
  );
}
