/*
 * kaifa
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using Asa.FaceControl;
using System.Web.Script.Serialization;

namespace ExtensionGroup
{
    public class KaiFa : IExtension
    {
        private int reelID = 0;
        private List<ExtensionControl> extensions;
        public event IExtension.PrintDelegate Printing;
        public event IExtension.PrintDelegate SaveRetrospect;

        public KaiFa() { }

        public void Clear()
        {
            Log.Info("Clear()");
            for (int i = 0; i < extensions.Count; i++)
            {
                if (!extensions[i].CanClear) continue;
                if (extensions[i].Control is FaceComboBox box)
                    box.Items.Clear();
                extensions[i].Control.Text = "";
            }
        }

        public void Dispose()
        {
        }

        public void Load(List<ExtensionControl> extensions)
        {
            this.extensions = extensions;
            ReadReelID();
        }

        public void SetKey(string[] originalCode, Dictionary<string, string> key)
        {
            for (int i = 0; i < extensions.Count; i++)
            {
                if (key.ContainsKey(extensions[i].Key))
                    extensions[i].Control.Text = key[extensions[i].Key];
            }

            if (CanPrint())
                PrintLabel(null, EventArgs.Empty);
        }

        public void Update()
        {
        }




        private bool CanPrint()
        {
            int index = extensions.FindIndex(match => match.Control.Name == "TxtPart");
            if (index == -1) return false;
            if (extensions[index].Control.Text == "") return false;
            index = extensions.FindIndex(match => match.Control.Name == "TxtQty");
            if (index == -1) return false;
            if (extensions[index].Control.Text == "") return false;

            return true;
        }

        private void ReadReelID()
        {
            try
            {
                if (System.IO.File.Exists(FilePath.CONFIG_REELID))
                {
                    string text = System.IO.File.ReadAllText(FilePath.CONFIG_REELID);
                    bool bln = int.TryParse(text, out reelID);
                    Log.Info($"ReadReelID {bln} text={text}");
                }
                else
                {
                    System.IO.FileStream fs = System.IO.File.Create(FilePath.CONFIG_REELID);
                    fs.Close();
                    reelID = 0;
                    Log.Info("ReadReelID Create");
                }
            }
            catch (Exception ex)
            {
                Log.Error("ReadReelID", ex);
                System.Windows.Forms.MessageBox.Show(ex.Message, "Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
            }
        }

        private void SaveAddReelID()
        {
            try
            {
                Log.Debug("打印标签前的ID:" + reelID);
                reelID++;
                System.IO.File.WriteAllText(FilePath.CONFIG_REELID, reelID.ToString());
                Log.Info($"Save ReelID:{reelID}");
            }
            catch (Exception ex)
            {
                Log.Error("SaveAddReelID error", ex);
            }
        }

        private void PrintLabel(object sender, EventArgs e)
        {
            Log.Debug("Enter PrintLabel Method");
            Dictionary<string, string> key = new();
            for (int i = 0; i < extensions.Count; i++)
            {
                if (extensions[i].Key == "") continue;
                if (key.ContainsKey(extensions[i].Key))
                    key[extensions[i].Key] = extensions[i].Control.Text;
                else
                    key.Add(extensions[i].Key, extensions[i].Control.Text);
            }

            SaveAddReelID();
            Printing?.Invoke(key);
        }

        public bool SetKey(string[] originalCode, Dictionary<string, string> key, bool hasMatch, out string errmsg)
        {
            throw new NotImplementedException();
        }

        public void Print(bool match, Dictionary<string, string> key)
        {
            throw new NotImplementedException();
        }

        public void DrawTextBackground(Dictionary<string, string> key)
        {
            throw new NotImplementedException();
        }
    }
}