using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using DH.Commons.Enums;
using HalconDotNet;
using OpenCvSharp;



namespace DH.Commons.Base
{
    /// <summary>
    /// 视觉处理引擎:1.传统视觉 2.深度学习
    /// CV深度学习 四大领域
    /// Image  Classification 图像分类:判别图中物体是什么,比如是猫还是狗;
    /// Semantic Segmentation 语义分割:对图像进行像素级分类,预测每个像素属于的类别,不区分个体;
    /// Object Detection 目标检测:寻找图像中的物体并进行定位;
    /// Instance Segmentation 实例分割:定位图中每个物体,并进行像素级标注,区分不同个体;
    /// </summary>
    public abstract class VisionEngineBase
    {
        public List<DetectionConfig> DetectionConfigs = new List<DetectionConfig>();
        #region event
        public event Action<string, List<double>> OnCropParamsOutput;
        public event Action<string, Bitmap, List<IShapeElement>> OnDetectionDone;
        public event Action<string> OnDetectionWarningStop;//有无检测 需要报警停机
        #endregion
        //public VisionEngineInitialConfigBase IConfig
        //{
        //    get => InitialConfig as VisionEngineInitialConfigBase;
        //}
        // public ImageSaveHelper ImageSaveHelper { get; set; } = new ImageSaveHelper();
        public string BatchNO { get; set; }

        public HTuple hv_ModelID;

        public abstract DetectStationResult RunInference(Mat originImgSet, string detectionId = null);

        //public abstract void SaveDetectResultAsync(DetectStationResult detectResult);



        public virtual void DetectionDone(string detectionId, Bitmap image, List<IShapeElement> detectionResults)
        {
            OnDetectionDone?.Invoke(detectionId, image, detectionResults);
        }

        public virtual void DetectionWarningStop(string detectionDes)
        {
            OnDetectionWarningStop?.Invoke(detectionDes);
        }

        public virtual void SaveImageAsync(string fullname, Bitmap saveMap, ImageFormat imageFormat)
        {
            if (saveMap != null)
            {
                //ImageSaveSet imageSaveSet = new ImageSaveSet()
                //{
                //    FullName = fullname,
                //    SaveImage = saveMap.CopyBitmap(),
                //    ImageFormat = imageFormat.DeepSerializeClone()
                //};

                //ImageSaveHelper.ImageSaveAsync(imageSaveSet);
            }
        }
    }
    public class CamModuleXY
    {
        [Category("图片行")]
        [DisplayName("行")]
        [Description("行")]
        // [TypeConverter(typeof(DeviceIdSelectorConverter<CameraBase>))]
        //[TypeConverter(typeof(CollectionCountConvert))]
        public int PicRows { get; set; } = 1;

        [Category("图片列")]
        [DisplayName("列")]
        [Description("列")]
        // [TypeConverter(typeof(DeviceIdSelectorConverter<CameraBase>))]
        //[TypeConverter(typeof(CollectionCountConvert))]
        public int PicCols { get; set; } = 1;

        public string GetDisplayText()
        {
            return "行:" + PicRows.ToString() + "列:" + PicCols.ToString();
        }
    }

    //public class RelatedCamera 
    //{

    //    [Category("关联相机")]
    //    [DisplayName("关联相机")]
    //    [Description("关联相机描述")]
    //    //[TypeConverter(typeof(DeviceIdSelectorConverter<CameraBase>))]
    //    //[TypeConverter(typeof(CollectionCountConvert))]
    //    public string CameraSourceId { get; set; } = "";


    //    //public string GetDisplayText()
    //    //{
    //    //    using (var scope = GlobalVar.Container.BeginLifetimeScope())
    //    //    {
    //    //        List<IDevice> deviceList = scope.Resolve<List<IDevice>>();
    //    //        IDevice CameraDevice = deviceList.FirstOrDefault(dev => dev.Id.Equals(CameraSourceId));

    //    //        if (CameraDevice != null && CameraDevice is CameraBase)
    //    //        {
    //    //            return CameraDevice.Name;
    //    //        }

    //    //    }
    //    //    return CameraSourceId;
    //    //}
    //}
    public class VisionEngineInitialConfigBase //: InitialConfigBase
    {
        [Category("深度学习检测配置")]
        [DisplayName("检测配置集合")]
        [Description("检测配置集合")]
        //[TypeConverter(typeof(CollectionCountConvert))]
        //[Editor(typeof(ComplexCollectionEditor<DetectionConfig>), typeof(UITypeEditor))]
        public List<DetectionConfig> DetectionConfigs { get; set; } = new List<DetectionConfig>();

        [Category("深度学习检测配置")]
        [DisplayName("标签分类")]
        [Description("标签分类,A_NG,B_TBD...")]
        // [TypeConverter(typeof(CollectionCountConvert))]
        // [Editor(typeof(ComplexCollectionEditor<RecongnitionLabelCategory>), typeof(UITypeEditor))]
        public List<RecongnitionLabelCategory> RecongnitionLabelCategoryList { get; set; } = new List<RecongnitionLabelCategory>();

        [Category("深度学习检测配置")]
        [DisplayName("检测标签定义集合")]
        [Description("定义检测标签的集合,例如:Seg/Detection模式:断裂、油污、划伤...;Class模式:ok、ng、上面、下面、套环、正常...")]
        //  [TypeConverter(typeof(CollectionCountConvert))]
        //  [Editor(typeof(ComplexCollectionEditor<RecongnitionLabel>), typeof(UITypeEditor))]
        public List<RecongnitionLabel> RecongnitionLabelList { get; set; } = new List<RecongnitionLabel>();

        [Category("深度学习检测配置")]
        [DisplayName("标签置信度")]
        [Description("标签置信度,过滤小于改置信度的标签,大于该设置的标签才能识别")]
        public float Score { get; set; } = 0.5f;

        [Category("深度学习检测配置")]
        [DisplayName("CPU线程数量")]
        [Description("用于深度学习的CPU线程数量,不要设置太大,会单独占用线程,影响其他程序运行")]
        public int CPUNums { get; set; } = 1;

        //[Category("深度学习检测配置")]
        //[DisplayName("检测项GPU指定")]
        //[Description("将检测项指定到GPU")]
        //  [TypeConverter(typeof(CollectionCountConvert))]
        //  [Editor(typeof(ComplexCollectionEditor<DetectionGPUConfig>), typeof(UITypeEditor))]
        // public List<DetectionGPUConfig> DetectionGPUList { get; set; } = new List<DetectionGPUConfig>();

        // [Category("数据保存配置")]
        //[DisplayName("是否保存检测明细CSV")]
        //[Description("是否保存 检测明细CSV")]
        //public override bool IsEnableCSV { get; set; } = true;

        //[Category("数据保存配置")]
        //[DisplayName("是否保存检测图片")]
        //[Description("是否保存 检测图片,总开关")]
        //public bool IsSaveImage { get; set; } = true;

        //[Category("数据保存配置")]
        //[Description("检测图片 保存文件夹")]
        //[DisplayName("检测图片保存文件夹")]
        //[Editor(typeof(FoldDialogEditor), typeof(UITypeEditor))]
        //public string ImageSaveDirectory { get; set; } = "D:\\PROJECTS\\X017\\Images";

        //[Category("数据保存配置")]
        //[Description("检测明细CSV文件夹")]
        //[DisplayName("检测明细CSV文件夹")]
        //[Editor(typeof(FoldDialogEditor), typeof(UITypeEditor))]
        //public string CSVDataPath { get; set; } = "D:\\PROJECTS\\X017\\Images";

    }
}