 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.ProcessMode;
using System.IO;
using DefaultNamespace.ProcessMode;
using MotionFramework;
using Newtonsoft.Json;

public class ProcessTest : MonoBehaviour
{
    private ProcessManager processManager;
    private string testJsonPath;
    private string testJsonContent;

    private IEnumerator Start()
    {
        yield return new WaitForSeconds(2);
        // 初始化测试
        InitializeTest();
    }

    private void InitializeTest()
    {
        // 获取ProcessManager实例
        processManager = MotionEngine.GetModule<ProcessManager>();

        // 设置测试JSON文件路径
        testJsonPath = Path.Combine(Application.streamingAssetsPath, "DataConfig", "流程测试.json");
        
        // 读取测试JSON文件
        if (File.Exists(testJsonPath))
        {
            testJsonContent = File.ReadAllText(testJsonPath);
            Debug.Log("成功加载测试流程文件");
        }
        else
        {
            Debug.LogError("测试流程文件不存在！");
            return;
        }

        // 开始测试
        StartCoroutine(RunAllTests());
    }

    private IEnumerator RunAllTests()
    {
        Debug.Log("开始运行流程系统测试...");

        // 测试教学模式
        yield return StartCoroutine(TestTeachingMode());

        // 测试培训模式
        yield return StartCoroutine(TestTrainingMode());

        // 测试练习模式
        yield return StartCoroutine(TestPracticeMode());

        // 测试考核模式
        yield return StartCoroutine(TestAssessmentMode());

        Debug.Log("所有测试完成！");
    }

    private IEnumerator TestTeachingMode()
    {
        Debug.Log("\n=== 开始测试教学模式 ===");
        
        // 重置流程
        processManager.ResetProcess(testJsonContent, ProcessMode.Teaching);
        
        // 测试顺序点击
        Debug.Log("测试顺序点击...");
        yield return StartCoroutine(TestSequentialClick());

        // 测试非顺序点击
        Debug.Log("测试非顺序点击...");
        yield return StartCoroutine(TestNonSequentialClick());

        // 测试事件类型目标
        Debug.Log("测试事件类型目标...");
        yield return StartCoroutine(TestEventTypeTarget());

        Debug.Log("教学模式测试完成");
    }

    private IEnumerator TestTrainingMode()
    {
        Debug.Log("\n=== 开始测试培训模式 ===");
        
        // 重置流程
        processManager.ResetProcess(testJsonContent, ProcessMode.Training);
        
        // 测试不需要正确完成的情况
        Debug.Log("测试不需要正确完成的情况...");
        yield return StartCoroutine(TestNoRequireCorrect());

        // 测试混合类型目标
        Debug.Log("测试混合类型目标...");
        yield return StartCoroutine(TestMixedTypeTargets());

        Debug.Log("培训模式测试完成");
    }

    private IEnumerator TestPracticeMode()
    {
        Debug.Log("\n=== 开始测试练习模式 ===");
        
        // 重置流程
        processManager.ResetProcess(testJsonContent, ProcessMode.Practice);
        
        // 测试重复目标
        Debug.Log("测试重复目标...");
        yield return StartCoroutine(TestDuplicateTargets());

        // 测试自由探索
        Debug.Log("测试自由探索...");
        yield return StartCoroutine(TestFreeExploration());

        Debug.Log("练习模式测试完成");
    }

    private IEnumerator TestAssessmentMode()
    {
        Debug.Log("\n=== 开始测试考核模式 ===");
        
        // 重置流程
        processManager.ResetProcess(testJsonContent, ProcessMode.Assessment);
        
        // 测试严格顺序
        Debug.Log("测试严格顺序...");
        yield return StartCoroutine(TestStrictSequential());

        // 测试综合场景
        Debug.Log("测试综合场景...");
        yield return StartCoroutine(TestComprehensiveScenario());

        Debug.Log("考核模式测试完成");
    }

    private IEnumerator TestSequentialClick()
    {
        // 模拟正确顺序点击
        processManager.HandleClick("Cube1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cube2");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cube3");
        yield return new WaitForSeconds(0.5f);

        // 测试错误顺序
        processManager.ResetProcess(testJsonContent, ProcessMode.Teaching);
        processManager.HandleClick("Cube2"); // 错误顺序
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestNonSequentialClick()
    {
        // 模拟任意顺序点击
        processManager.HandleClick("Sphere2");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Sphere1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Sphere3");
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestEventTypeTarget()
    {
        // 测试事件类型目标
        processManager.HandleClick("Event1");
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestNoRequireCorrect()
    {
        // 测试不需要正确完成的情况
        processManager.HandleClick("WrongObject"); // 错误点击
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cylinder1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cylinder2");
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestMixedTypeTargets()
    {
        // 测试混合类型目标
        processManager.HandleClick("Pyramid1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Event2");
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestDuplicateTargets()
    {
        // 测试重复目标
        processManager.HandleClick("Cube1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cube1"); // 重复点击
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cube2");
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestFreeExploration()
    {
        // 测试自由探索
        processManager.HandleClick("Sphere1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Event3");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Sphere2");
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestStrictSequential()
    {
        // 测试严格顺序
        processManager.HandleClick("Cube1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cube2");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cube3");
        yield return new WaitForSeconds(0.5f);
    }

    private IEnumerator TestComprehensiveScenario()
    {
        // 测试综合场景
        processManager.HandleClick("Sphere1");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Event4");
        yield return new WaitForSeconds(0.5f);
        processManager.HandleClick("Cylinder1");
        yield return new WaitForSeconds(0.5f);
    }

    // 用于在编辑器中手动触发测试
    [ContextMenu("运行所有测试")]
    public void RunTests()
    {
        StartCoroutine(RunAllTests());
    }
}