using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
/// <summary>
/// 数据管理类
/// </summary>
public class DataManager : BaseManager<DataManager>
{
    private DataManager() { }
    /// <summary>
    /// 方案
    /// </summary>
    private Dictionary<int, TB_Scheme> schemeDic = new Dictionary<int, TB_Scheme>();

    /// <summary>
    /// 流程
    /// </summary>
    private Dictionary<int, TB_Process> processDic = new Dictionary<int, TB_Process>();

    /// <summary>
    /// 子流程
    /// </summary>
    private Dictionary<int, TB_SubProcess> subProcessDic = new Dictionary<int, TB_SubProcess>();

    /// <summary>
    /// 子流程步骤
    /// </summary>
    private Dictionary<int, TB_SubProcessStep> subProcessStepDic = new Dictionary<int, TB_SubProcessStep>();

    /// <summary>
    /// 用户数据
    /// </summary>
    public UserData userData;

    /// <summary>
    /// 初始化
    /// </summary>
    public void Init() 
    {
        schemeDic = GameManager.BinaryMgr.GetTable<TB_SchemeContainer>().dataDic;
        processDic = GameManager.BinaryMgr.GetTable<TB_ProcessContainer>().dataDic;
        subProcessDic = GameManager.BinaryMgr.GetTable<TB_SubProcessContainer>().dataDic;
        subProcessStepDic = GameManager.BinaryMgr.GetTable<TB_SubProcessStepContainer>().dataDic;
    }

    /// <summary>
    /// 获取D_Scheme
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public D_Scheme GetSchemeData(int id) 
    {
        D_Scheme d_Scheme = new D_Scheme();
        //获取方案数据
        if (schemeDic.ContainsKey(id))
        {
            d_Scheme.id = schemeDic[id].id;
            d_Scheme.schemeName = schemeDic[id].schemeName;
        }
        d_Scheme.processes = GetD_Processes(d_Scheme.id);
        return d_Scheme;
    }

    /// <summary>
    /// 根据方案Id 获取流程列表
    /// </summary>
    /// <param name="schemeId"></param>
    /// <returns></returns>
    private List<D_Process> GetD_Processes(int schemeId) 
    {
        List<D_Process> d_Processes = new List<D_Process>();
        //获取方案流程数据
        foreach (var item in processDic.Values)
        {
            if (item.schemeId == schemeId)
            {
                D_Process d_Process = new D_Process();
                d_Process.id = item.id;
                d_Process.processName = item.processName;
                int processId = item.id;
                d_Process.subProcesses = GetD_SubProcesses(schemeId, processId);
                d_Processes.Add(d_Process);
            }
        }
        return d_Processes;
    }

    /// <summary>
    /// 根据方案Id,流程Id ,获取子流程列表
    /// </summary>
    /// <param name="schemeId"></param>
    /// <param name="processId"></param>
    /// <returns></returns>
    public List<D_SubProcess> GetD_SubProcesses(int schemeId, int processId)
    {
        List<D_SubProcess> d_SubProcesses = new List<D_SubProcess>();
        foreach(var item in subProcessDic.Values) 
        {
            if (item.schemeId == schemeId && item.processId == processId)
            {
                D_SubProcess d_SubProcess = new D_SubProcess();
                d_SubProcess.id = item.id;
                d_SubProcess.schemeId = item.schemeId;
                d_SubProcess.processId = item.processId;
                d_SubProcess.isPrecondition = item.isPrecondition;
                d_SubProcess.precondition = item.precondition;
                d_SubProcess.tips = item.tips;
                int subProcessId = item.id;
                d_SubProcess.subProcessStepes = GetD_SubProcessSteps(schemeId, processId, subProcessId);
                d_SubProcesses.Add(d_SubProcess);
            }
        }
        return d_SubProcesses;
    }

    /// <summary>
    /// 根据方案Id,流程Id,子流程Id  获取子流程步骤列表
    /// </summary>
    /// <param name="schemeId"></param>
    /// <param name="processId"></param>
    /// <param name="subProcessStepId"></param>
    /// <returns></returns>
    private List<D_SubProcessStep> GetD_SubProcessSteps(int schemeId, int processId,int subProcessId) 
    {
        List<D_SubProcessStep> d_SubProcessSteps = new List<D_SubProcessStep>();
        foreach(var step in subProcessStepDic.Values) 
        {
            if (step.schemeId == schemeId && step.processId == processId && step.subProcessId == subProcessId)
            {
                D_SubProcessStep d_SubProcessStep = new D_SubProcessStep();
                d_SubProcessStep.id = step.id;
                d_SubProcessStep.schemeId = step.schemeId;
                d_SubProcessStep.processId = step.processId;
                d_SubProcessStep.subProcessId = step.subProcessId;
                d_SubProcessStep.subProcessStepName = step.subProcessStepName;
                d_SubProcessStep.isPrecondition = step.isPrecondition;
                d_SubProcessStep.precondition = step.precondition;
                d_SubProcessStep.score = step.score;
                d_SubProcessSteps.Add(d_SubProcessStep);
            }
        }
        return d_SubProcessSteps;
    }

    /// <summary>
    /// 获取报告
    /// </summary>
    /// <param name="id"></param>
    public R_Scheme GetSchemeReport(int id) 
    {
        R_Scheme r_Scheme = new R_Scheme();


        return r_Scheme;
    }

}
