import React, {
  createContext,
  useState,
  useContext,
  ReactNode,
  useEffect,
} from 'react';
import { Task } from '../types/task';
import {
  AppConfig,
  LocationOption,
  PayloadOption,
  RobotActionOption,
} from '../types/config';
import {
  getConfig,
  getSettings,
  executeTask,
  clearCachedConfig,
} from '../services/configService';

interface TasksContextData {
  tasks: Task[];
  locations: LocationOption[];
  locationsBays: LocationOption[];
  payloads: PayloadOption[];
  robotActions: RobotActionOption[];
  serverUrl: string | null;
  getTaskById: (id: string) => Task | undefined;
  updateTask: (updatedTask: Task) => void;
  runTask: (id: string) => void;
  refreshConfig: () => Promise<void>;
  isConfigLoaded: boolean;
}

const TasksContext = createContext<TasksContextData>({} as TasksContextData);

export const TasksProvider: React.FC<{ children: ReactNode }> = ({
  children,
}) => {
  const [tasks, setTasks] = useState<Task[]>([]);
  const [locations, setLocations] = useState<LocationOption[]>([]);
  const [locationsBays, setLocationsBays] = useState<LocationOption[]>([]);
  const [payloads, setPayloads] = useState<PayloadOption[]>([]);
  const [robotActions, setRobotActions] = useState<RobotActionOption[]>([]);
  const [isConfigLoaded, setIsConfigLoaded] = useState(false);
  const [serverUrl, setServerUrl] = useState<string | null>(null);

  const fetchTasks = async (baseUrl: string, endpoint: string) => {
    try {
      if (baseUrl && endpoint) {
        const fetchUrl = `${baseUrl}${endpoint}?pageNum=1&pageSize=100`;
        console.log('Fetching tasks from:', fetchUrl);
        const response = await fetch(fetchUrl);

        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }

        const responseData = await response.json();
        console.log('responseData', responseData);

        if (responseData && responseData.code === 200) {
          const fetchedTasks = responseData.data.list.map((task: any) => {
            let detail = task.detail;
            if (detail && typeof detail === 'string') {
              try {
                detail = JSON.parse(detail);
              } catch (e) {
                console.error('解析任务详情失败 (list):', e);
                detail = null;
              }
            }
            return {
              ...task,
              name: task.label,
              parameters: task.parameters || {}, // Ensure parameters object exists
              detail: detail,
            };
          });
          setTasks(fetchedTasks);
        } else {
          console.error('获取任务列表失败: responseData.code is not 200');
          setTasks([]); //
        }
      }
    } catch (error) {
      console.error('获取任务列表失败:', error);
      if (error instanceof Error) {
        console.error('Error message:', error.message);
      }
      setTasks([]); //
    }
  };

  useEffect(() => {
    const loadApp = async () => {
      try {
        await clearCachedConfig();
        const config = await getConfig();
        if (config) {
          applyConfig(config); // Don't load tasks from config
          setIsConfigLoaded(true);
          if (config.serverUrl && config.apiEndpoints) {
            await fetchTasks(config.serverUrl, config.apiEndpoints.getTasks);
          }
        } else {
          console.log('没有找到配置文件，使用空数据');
          setIsConfigLoaded(false);
        }
      } catch (error) {
        console.error('加载配置失败:', error);
        setIsConfigLoaded(false);
      }
    };

    loadApp();
  }, []);

  const [apiEndpoints, setApiEndpoints] = useState<{
    getTasks: string;
    getTaskDetail: string;
    runTask: string;
  } | null>(null);

  const applyConfig = (config: AppConfig) => {
    setLocations(config.locations || []);
    setLocationsBays(config.locationsBays || []);
    setPayloads(config.payloads || []);
    setRobotActions(config.robotActions || []);
    if (config.serverUrl) {
      setServerUrl(config.serverUrl);
    }
    if (config.apiEndpoints) {
      setApiEndpoints(config.apiEndpoints);
    }
  };

  const refreshConfig = async () => {
    try {
      await clearCachedConfig();
      const config = await getConfig();
      if (config) {
        applyConfig(config, false); // Don't load tasks from config
        setIsConfigLoaded(true);
        if (config.serverUrl && config.apiEndpoints) {
          await fetchTasks(config.serverUrl, config.apiEndpoints.getTasks);
        }
      } else {
        console.log('刷新配置时没有找到配置文件');
        setIsConfigLoaded(false);
      }
    } catch (error) {
      console.error('刷新配置失败:', error);
      setIsConfigLoaded(false);
    }
  };

  const getTaskById = (id: string) => {
    const task = tasks.find(task => task.id === id);
    if (task && !task.detail) {
      fetchTaskDetail(id);
    }
    return task;
  };

  const fetchTaskDetail = async (taskId: string) => {
    try {
      if (serverUrl && apiEndpoints) {
        const endpoint = apiEndpoints.getTaskDetail.replace('{taskId}', taskId);
        const url = `${serverUrl}${endpoint}`;
        console.log('Fetching task detail from:', url);
        const response = await fetch(url);

        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }

        const responseData = await response.json();
        console.log('task detail responseData', responseData);

        if (responseData && responseData.code === 200) {
          let taskDetail = responseData.data.detail;
          if (taskDetail && typeof taskDetail === 'string') {
            try {
              taskDetail = JSON.parse(taskDetail);
            } catch (e) {
              console.error('解析任务详情失败 (detail):', e);
              taskDetail = null;
            }
          }
          setTasks(prevTasks =>
            prevTasks.map(task =>
              task.id === taskId ? { ...task, detail: taskDetail } : task,
            ),
          );
        } else {
          console.error(
            '获取任务详情失败: responseData.code is not 200',
          );
        }
      }
    } catch (error) {
      console.error('获取任务详情失败:', error);
    }
  };

  const updateTask = (updatedTask: Task) => {
    setTasks(prevTasks =>
      prevTasks.map(task => (task.id === updatedTask.id ? updatedTask : task)),
    );
  };

  const runTask = async (id: string) => {
    const task = getTaskById(id);
    if (!task || !serverUrl || !apiEndpoints) return;

    // 更新任务状态为运行中
    setTasks(prevTasks =>
      prevTasks.map(t => (t.id === id ? { ...t, status: 1 } : t)),
    );

    try {
      // 提取参数值
      const parameters = Object.entries(task.parameters).reduce(
        (acc, [key, param]) => {
          if (param) {
            acc[key] = param.value;
          }
          return acc;
        },
        {} as { [key: string]: any },
      );

      // 获取服务器设置并发送任务执行请求
      const endpoint = apiEndpoints.runTask.replace('{taskId}', task.id);
      await executeTask(serverUrl, endpoint, {
        name: task.name,
        parameters: parameters,
      });

      // 模拟任务完成（实际项目中应该通过WebSocket或轮询获取任务状态）
      setTimeout(() => {
        setTasks(prevTasks =>
          prevTasks.map(t => (t.id === id ? { ...t, status: 2 } : t)),
        );
      }, 5000);
    } catch (error) {
      console.error('任务执行失败:', error);
      // 任务执行失败，更新状态为错误
      setTasks(prevTasks =>
        prevTasks.map(t => (t.id === id ? { ...t, status: 3 } : t)),
      );
    }
  };

  return (
    <TasksContext.Provider
      value={{
        tasks,
        locations,
        locationsBays,
        payloads,
        robotActions,
        serverUrl,
        getTaskById,
        updateTask,
        runTask,
        refreshConfig,
        isConfigLoaded,
      }}
    >
      {children}
    </TasksContext.Provider>
  );
};

export function useTasks() {
  return useContext(TasksContext);
}
