using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

/// <summary>
/// 流程类
/// </summary>
public class D_Process : I_Enter, I_Exit
{
    #region 变量
    /// <summary>
    /// 自身Id
    /// </summary>
    public int id;

    /// <summary>
    /// 流程Id
    /// </summary>
    public int schemeId;

    /// <summary>
    /// 流程名称
    /// </summary>
    public string processName;

    #endregion


    /// <summary>
    /// 存放流程的子流程
    /// </summary>
    public List< D_SubProcess> subProcesses = new List<D_SubProcess>();

    /// <summary>
    /// 当前子流程
    /// </summary>
    private D_SubProcess currentSubProcess;

    /// <summary>
    /// 当前子流程
    /// </summary>
    public D_SubProcess CurrentSubProcess 
    { 
        get => currentSubProcess;
        set 
        {
            currentSubProcess = value;
            CurrentSubProcessId = currentSubProcess.id;
        }
    }

    /// <summary>
    /// 当前子流程Id
    /// </summary>
    private int currentSubProcessId;

    /// <summary>
    /// 当前子流程Id
    /// </summary>
    public int CurrentSubProcessId 
    {
        get => currentSubProcessId; 
        set 
        {
            currentSubProcessId = value;
            GameManager.ProcessMgr.subProcessId = currentSubProcessId;
        }
    }

    /// <summary>
    /// 初始化
    /// </summary>
    public void Init() 
    {
        if (subProcesses == null || subProcesses.Count == 0)
            return;
        CurrentSubProcess = subProcesses[0];
        CurrentSubProcess.Init();
    }

    /// <summary>
    /// 切换子流程
    /// </summary>
    public void HandoverSubProcess(int subProcessId,int subProcessStepId)
    {
        if (CurrentSubProcess!= null)
        CurrentSubProcess.Exit();
        CurrentSubProcess = GetSubProcess(subProcessId);
        CurrentSubProcess.Enter();
        UnityEngine.Debug.Log("切换子流程到:" + subProcessId);
        CurrentSubProcess.HandoverSubProcessStep(subProcessStepId);
    }

    /// <summary>
    /// 根据id获取 子流程对象
    /// </summary>
    /// <param name="processId"></param>
    /// <returns></returns>
    private D_SubProcess GetSubProcess(int subProcessId)
    {
        for (int i = 0; i < subProcesses.Count; i++)
        {
            if (subProcesses[i].id == subProcessId)
            {
                return subProcesses[i];
            }
        }
        return null;
    }

    /// <summary>
    /// 进入流程
    /// </summary>
    public void Enter()
    {
        UnityEngine.Debug.Log("进入当前流程:" + id + "_" + processName);
    }

    /// <summary>
    /// 退出流程
    /// </summary>
    public void Exit()
    {
        UnityEngine.Debug.Log("退出当前流程:" + id +"_" + processName);
    }
}
