using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using wispro.sp.entity.workflowDefine;
using wispro.sp.share.Utility;

namespace wispro.sp.web.Components
{
    public partial class FlowChart
    {
        share.Utility.FlowChartUtility flowChartUtility;
        [Parameter]
        public double TitleHeight { get; set; } = 100;

        [Parameter]
        public double ChartWidth { get; set; } = 1200;
        [Parameter]
        public double ChartHeight { get; set; } = 900;
        [Parameter]
        public double rectWidth { get; set; } = 150;

        [Parameter]
        public double rectHeight { get; set; } = 60;
        [Parameter]
        public double initRadius { get; set; } = 20;
        [Parameter]
        public double EndRadius { get; set; } = 20;
        [Parameter]
        public double hSeparation { get; set; } = 40;

        [Parameter]
        public string StepShapeColor { get; set; } = "green";

        [Parameter]
        public string EndShapColor { get; set; } = "gray";

        int rectFontSize { get; set; } = 18;

        [Parameter]
        public List<entity.workflowDefine.Step> Steps { get; set; }

        [Parameter]
        public List<entity.workflowDefine.TrasferCondition> Transfers { get; set; }

        [Parameter]
        public List<entity.workflowDefine.Action> Actions { get; set; }

        [Parameter]
        public entity.workflowDefine.Workflow workflow { get; set; }

        protected  override  void OnInitialized()
        {
            Refresh();
        }
        

        public void Refresh()
        {
            flowChartUtility = new share.Utility.FlowChartUtility();
            flowChartUtility.workflow = workflow;
            flowChartUtility.Actions = Actions;
            flowChartUtility.Steps = Steps;
            flowChartUtility.Transfers = Transfers;
            flowChartUtility.hSeparation = hSeparation;
            flowChartUtility.initRadius = initRadius;
            flowChartUtility.EndShapColor = EndShapColor;
            flowChartUtility.EndRadius = EndRadius;
            flowChartUtility.ChartHeight = ChartHeight;
            flowChartUtility.ChartWidth = ChartWidth;
            flowChartUtility.rectHeight = rectHeight;
            flowChartUtility.rectWidth = rectWidth;
            flowChartUtility.Refresh();

            StateHasChanged();
        }

        void InitAction()
        {

        }

        #region 单击/双击事件
        [Parameter]
        public EventCallback<entity.workflowDefine.Step> OnClickStep { get; set; }

        [Parameter]
        public EventCallback<entity.workflowDefine.Action> OnClickAction{ get; set; }

        [Parameter]
        public EventCallback<entity.workflowDefine.TrasferCondition> OnClickTransfer { get; set; }

        [Parameter]
        public EventCallback<entity.workflowDefine.Step> OnDoubleClickStep { get; set; }

        [Parameter]
        public EventCallback<entity.workflowDefine.Action> OnDoubleClickAction { get; set; }

        [Parameter]
        public EventCallback<entity.workflowDefine.TrasferCondition> OnDoubleClickTransfer { get; set; }

        public dynamic SelectedObject;

        void ClickNode(shapeNode node)
        {
            SelectedObject = node.NodeObject;
            flowChartUtility.SelectedShape = node;
            StateHasChanged();

            if (node.NodeObject is entity.workflowDefine.Step)
            {
                if (OnClickStep.HasDelegate)
                {
                    OnClickStep.InvokeAsync((entity.workflowDefine.Step)node.NodeObject);
                }
            }

            if (node.NodeObject is entity.workflowDefine.Action)
            {
                if (OnClickAction.HasDelegate)
                {
                    OnClickAction.InvokeAsync((entity.workflowDefine.Action)node.NodeObject);
                }
            }
        }

        void DoubleClickNode(shapeNode node)
        {
            SelectedObject = node.NodeObject;
            flowChartUtility.SelectedShape = node;
            //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(node.NodeObject));
            
            if (node.NodeObject is entity.workflowDefine.Step)
            {
                if (OnDoubleClickStep.HasDelegate)
                {
                    OnDoubleClickStep.InvokeAsync((entity.workflowDefine.Step)node.NodeObject);
                }
            }

            if (node.NodeObject is entity.workflowDefine.Action)
            {
                
                if (OnDoubleClickAction.HasDelegate)
                {
                    OnDoubleClickAction.InvokeAsync((entity.workflowDefine.Action)node.NodeObject);
                }
            }

            StateHasChanged();
        }

        void ClickTrasfer(entity.workflowDefine.TrasferCondition trasferCondition)
        {
            SelectedObject = trasferCondition;
            flowChartUtility.SelectedShape = null;

            if (OnClickTransfer.HasDelegate)
            {
                OnClickTransfer.InvokeAsync(trasferCondition);
            }
        }

        void DoubleClickTrasfer(entity.workflowDefine.TrasferCondition trasferCondition)
        {
            SelectedObject = trasferCondition;
            flowChartUtility.SelectedShape = null;

            if (OnDoubleClickTransfer.HasDelegate)
            {
                OnDoubleClickTransfer.InvokeAsync(trasferCondition);
            }
        }
        #endregion

    }

}
