"use client";

import { useApp } from "@/contexts/AppContext";
import { useEffect, useRef } from "react";
import * as echarts from "echarts";

export default function AssetDistribution() {
  const { t } = useApp();
  const chartRef = useRef<HTMLDivElement>(null);

  const data = [
    {
      value: 52.7,
      name: t("transparency.fixedIncome"),
      itemStyle: {
        color: "#1447e6",
      },
    },
    {
      value: 30.1,
      name: t("transparency.alternativeAssets"),
      itemStyle: {
        color: "#10b981",
      },
    },
    {
      value: 17.2,
      name: t("transparency.alternativeCredit"),
      itemStyle: {
        color: "#ff6900",
      },
    },
  ];

  useEffect(() => {
    if (!chartRef.current) return;

    const chart = echarts.init(chartRef.current, null, {
      renderer: "svg",
    });

    const option = {
      tooltip: {
        trigger: "item",
        backgroundColor: "rgba(255, 255, 255, 0.95)",
        borderColor: "rgba(0, 0, 0, 0.1)",
        borderWidth: 1,
        padding: [12, 16],
        textStyle: {
          color: "#0f172b",
          fontSize: 13,
        },
        formatter: (params: any) => {
          return `
            <div style="font-weight: 600; margin-bottom: 4px;">${params.name}</div>
            <div style="color: ${params.color}; font-weight: 700; font-size: 15px;">${params.data.value}%</div>
          `;
        },
      },
      legend: {
        top: "bottom",
        left: "center",
        itemWidth: 12,
        itemHeight: 12,
        itemGap: 16,
        textStyle: {
          fontSize: 13,
          color: "#64748b",
        },
        icon: "circle",
        inactiveColor: "#cbd5e1",
      },
      series: [
        {
          name: "Asset Distribution",
          type: "pie",
          radius: ["40%", "70%"],
          center: ["50%", "42%"],
          avoidLabelOverlap: true,
          padAngle: 6,
          itemStyle: {
            borderRadius: 12,
            borderColor: "#fff",
            borderWidth: 3,
          },
          label: {
            show: true,
            position: "outside",
            alignTo: "none",
            distanceToLabelLine: 5,
            formatter: (params: any) => {
              return `{name|${params.name}}\n{value|${params.value}%}`;
            },
            rich: {
              name: {
                fontSize: 13,
                fontWeight: "600",
                color: "#0f172b",
                lineHeight: 18,
              },
              value: {
                fontSize: 14,
                fontWeight: "bold",
                lineHeight: 20,
              },
            },
          },
          labelLine: {
            show: true,
            length: 20,
            length2: 40,
            smooth: true,
            lineStyle: {
              width: 1.5,
            },
          },
          emphasis: {
            scale: true,
            scaleSize: 8,
            itemStyle: {
              shadowBlur: 20,
              shadowColor: "rgba(0, 0, 0, 0.2)",
            },
            label: {
              show: true,
              formatter: (params: any) => {
                return `{name|${params.name}}\n{value|${params.value}%}`;
              },
              fontSize: 15,
              fontWeight: "bold",
              color: "#0f172b",
              rich: {
                name: {
                  fontSize: 15,
                  fontWeight: "bold",
                  color: "#0f172b",
                  lineHeight: 22,
                },
                value: {
                  fontSize: 16,
                  fontWeight: "bold",
                  lineHeight: 24,
                },
              },
            },
            labelLine: {
              show: true,
              length: 20,
              length2: 40,
              lineStyle: {
                width: 2,
              },
            },
          },
          data: data,
          animationType: "expansion",
          animationEasing: "cubicOut",
          animationDuration: 800,
        },
      ],
    };

    chart.setOption(option);

    const handleResize = () => {
      chart.resize();
    };
    window.addEventListener("resize", handleResize);

    return () => {
      window.removeEventListener("resize", handleResize);
      chart.dispose();
    };
  }, [t]);

  return (
    <div className="flex-1 bg-bg-surface dark:bg-gray-800 rounded-3xl border border-border-gray dark:border-gray-700 p-6 flex flex-col gap-6">
      {/* Header */}
      <div className="flex flex-col gap-0">
        <h3 className="text-[20px] font-bold text-text-primary dark:text-white leading-[140%]">
          {t("transparency.assetDistribution")}
        </h3>
        <p className="text-caption-tiny font-regular text-text-tertiary dark:text-gray-400 leading-[150%] tracking-[0.01em]">
          {t("transparency.distributionSubtitle")}
        </p>
      </div>

      {/* Pie Chart Section */}
      <div className="flex flex-col gap-3">
        {/* ECharts Pie Chart */}
        <div className="relative h-[320px] w-full overflow-visible">
          <div ref={chartRef} className="w-full h-full" />
        </div>
      </div>
    </div>
  );
}
