import { IconSymbol } from "@/components/ui/icon-symbol";
import { useTrip } from "@/state/use-trip";
import React, { useRef, useState } from "react";
import { Animated, Text, TouchableOpacity, View } from "react-native";
import NetDetailModal from "./modal/NetDetailModal/NetDetailModal";
import styles from "./style/NetListTable.styles";

const NetListTable: React.FC = () => {
  const [collapsed, setCollapsed] = useState(true);
  const [contentHeight, setContentHeight] = useState<number>(0);
  const animatedHeight = useRef(new Animated.Value(0)).current;
  const [modalVisible, setModalVisible] = useState(false);
  const [selectedNet, setSelectedNet] = useState<Model.FishingLog | null>(null);
  const { trip } = useTrip();
  const data: Model.FishingLog[] = trip?.fishing_logs ?? [];
  const tongSoMe = data.length;

  const handleToggle = () => {
    const toValue = collapsed ? contentHeight : 0;
    Animated.timing(animatedHeight, {
      toValue,
      duration: 300,
      useNativeDriver: false,
    }).start();
    setCollapsed((prev) => !prev);
  };

  const handleStatusPress = (id: string) => {
    const net = data.find((item) => item.fishing_log_id === id);
    if (net) {
      setSelectedNet(net);
      setModalVisible(true);
    }
  };

  return (
    <View style={styles.container}>
      {/* Header toggle */}
      <TouchableOpacity
        activeOpacity={0.7}
        onPress={handleToggle}
        style={styles.headerRow}
      >
        <Text style={styles.title}>Danh sách mẻ lưới</Text>
        {collapsed && <Text style={styles.totalCollapsed}>{tongSoMe}</Text>}
        <IconSymbol
          name={collapsed ? "chevron.down" : "chevron.up"}
          size={16}
          color="#000"
        />
      </TouchableOpacity>

      {/* Nội dung ẩn để đo chiều cao */}
      <View
        style={{ position: "absolute", opacity: 0, zIndex: -1000 }}
        onLayout={(event) => {
          const height = event.nativeEvent.layout.height;
          if (height > 0 && contentHeight === 0) {
            setContentHeight(height);
          }
        }}
      >
        {/* Header */}
        <View style={[styles.row, styles.tableHeader]}>
          <Text style={[styles.sttCell, styles.headerText]}>STT</Text>
          <Text style={[styles.cell, styles.headerText]}>Trạng thái</Text>
        </View>

        {/* Body */}
        {data.map((item, index) => (
          <View key={item.fishing_log_id} style={styles.row}>
            {/* Cột STT */}
            <Text style={styles.sttCell}>Mẻ {index + 1}</Text>

            {/* Cột Trạng thái */}
            <View style={[styles.cell, styles.statusContainer]}>
              <View style={styles.statusDot} />
              <TouchableOpacity
                onPress={() => handleStatusPress(item.fishing_log_id)}
              >
                <Text style={styles.statusText}>
                  {item.status ? "Đã hoàn thành" : "Chưa hoàn thành"}
                </Text>
              </TouchableOpacity>
            </View>
          </View>
        ))}
      </View>

      {/* Bảng hiển thị với animation */}
      <Animated.View style={{ height: animatedHeight, overflow: "hidden" }}>
        {/* Header */}
        <View style={[styles.row, styles.tableHeader]}>
          <Text style={[styles.sttCell, styles.headerText]}>STT</Text>
          <Text style={[styles.cell, styles.headerText]}>Trạng thái</Text>
        </View>

        {/* Body */}
        {data.map((item, index) => (
          <View key={item.fishing_log_id} style={styles.row}>
            {/* Cột STT */}
            <Text style={styles.sttCell}>Mẻ {index + 1}</Text>

            {/* Cột Trạng thái */}
            <View style={[styles.cell, styles.statusContainer]}>
              <View style={styles.statusDot} />
              <TouchableOpacity
                onPress={() => handleStatusPress(item.fishing_log_id)}
              >
                <Text style={styles.statusText}>
                  {item.status ? "Đã hoàn thành" : "Chưa hoàn thành"}
                </Text>
              </TouchableOpacity>
            </View>
          </View>
        ))}
      </Animated.View>

      {/* Modal chi tiết */}
      <NetDetailModal
        visible={modalVisible}
        onClose={() => {
          console.log("OnCLose");
          setModalVisible(false);
        }}
        netData={selectedNet}
        stt={
          selectedNet
            ? data.findIndex(
                (item) => item.fishing_log_id === selectedNet.fishing_log_id
              ) + 1
            : undefined
        }
      />
    </View>
  );
};

export default NetListTable;
