using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using PostCardHitsuji.DataClient.DAO;
using PostCardHitsuji.DataClient.Model;
using PostCardHitsuji.Ui.RowPanel;
using Hitsuji.Framework.Thread;
using Hitsuji.Framework.Utils;
using PostCardHitsuji.Global;
using PostCardHitsuji.Utils.WinControlAttacher;

namespace PostCardHitsuji.Ui.Controls
{
    public partial class PostCardListView : UserControl
    {
        public event ListViewItemSelectionChangedEventHandler OnListItemSelectedEvent;


        private Dictionary<string, IllustLayoutContentModel> layouts
        {
            get;set;
        }

        public PostCardListView()
        {
            InitializeComponent();
            this.listViewLayout.MultiSelect = false;
            this.Load += new EventHandler(onLoad);
        }

        private void onLoad(object sender, EventArgs ev)
        {
            this.listViewLayout.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(listViewLayout_ItemSelectionChanged);
        }

        public void SearchPostCardAll()
        {
            var paramHash = new System.Collections.Hashtable();
            var param = new IllustLayoutContentModel();
            param.Enable = true.ToString().ToUpper();
            paramHash.Add("param", param);
            paramHash.Add("callback_resLayout", new Action<object>(callback));
            Hitsuji.Framework.Thread.ThreadRunner.doProcess(paramHash, doSearch, "search_postCard", true);
        }

        private void doSearch(object paramHash)
        {
            var model = (paramHash as System.Collections.Hashtable)["param"] as IllustLayoutContentModel;
            var callback = (paramHash as System.Collections.Hashtable)["callback_resLayout"] as Action<object>;
            using (var daoFactory = new DaoFactory())
            {
                try
                {
                    #region レイアウト情報
                    var resLayoutDao = (IllustLayoutDao)daoFactory.getDao<IllustLayoutContentModel>(GlobalSettings.illLayoutDao_);
                    resLayoutDao.inModel = model;
                    var resLayoutList = resLayoutDao.selectSimple(null) as List<IllustLayoutContentModel>;
                    #endregion

                    if (callback != null)
                        callback(resLayoutList);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            }
        }

        private void callback(object outputList)
        {
            layouts  = new Dictionary<string,IllustLayoutContentModel>();
            foreach (var layout in outputList as List<IllustLayoutContentModel>)
                layouts.Add(layout.layoutName, layout);

            Action bindAction = delegate
            {
                //アイテムを全てクリア
                if (listViewLayout.Items != null)
                    listViewLayout.Items.Clear();
                listViewLayout.LargeImageList = null;

                var width_ = (int)(GlobalSettings.PaperVertical.PAPER_WIDTH * 0.4);
                var height_ = (int)(GlobalSettings.PaperVertical.PAPER_HEIGHT * 0.4);

                //前段で取得した非選択アイテムをリストビューに追加
                listViewLayout.LargeImageList = new ImageList();
                listViewLayout.LargeImageList.ImageSize = new Size(width_, height_);

                NewPaperLayer2 paper = null;
                foreach (var item in outputList as List<IllustLayoutContentModel>)
                {
                    paper = new NewPaperLayer2();
                    paper.DrawType = NewPaperLayer2.DrawTypes.RESEIVER_LAYOUT;
                    foreach (var msg in item.MsgItemList)
                        paper.addMsgItem(msg.Name, msg, msg.Name.IndexOf("RESEIVER") >= 0 ? GlobalSettings.RESEIVER_RECTABGLE_COLOR : GlobalSettings.SENDER_RECTABGLE_COLOR);
                    
                    ListViewAttacher.addLargeItem(listViewLayout,
                        paper.saveImage(true),
                        (int)(GlobalSettings.PaperVertical.PAPER_WIDTH * 0.4),
                        (int)(GlobalSettings.PaperVertical.PAPER_HEIGHT * 0.4),
                        item.layoutName);
                }
            };

            if (this.InvokeRequired) this.Invoke(bindAction);
            else bindAction();
        }

        public IllustLayoutContentModel getLayoutModelByName(string name)
        {
            foreach (var kvp in layouts)
                if (kvp.Key.Equals(name)) return kvp.Value;
            return null;
        }

        private void listViewLayout_ItemSelectionChanged(object listView, ListViewItemSelectionChangedEventArgs ev)
        {
            if (OnListItemSelectedEvent != null)
                OnListItemSelectedEvent(listView, ev);
        }
    }
}
