using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonAI.ZoneEditor;
using CommonLang.Property;
using CommonAI.Zone.ZoneEditor;
using CommonAI.Zone.ZoneEditor.EventTrigger;
using System.Reflection;
using CommonLang;
using CommonFroms.DescAttributeEdit;
using CommonLang.Xml;
using System.Collections;

namespace CommonAIEditor.Scene
{
    public partial class SceneVarDialog : Form
    {
        readonly private SceneEditor sceneEditor;
        readonly private SceneVarEditor varEdit;
        readonly private bool IsAdd;
        readonly private ZoneVar SrcVar;
        private ZoneVar mData;
        public ZoneVar Value
        {
            get
            {
                return mData;
            }
        }

        public SceneVarDialog(SceneEditor sedit, SceneVarEditor vedit, ZoneVar var = null)
        {
            InitializeComponent();
            this.varEdit = vedit;
            this.sceneEditor = sedit;
            this.IsAdd = var == null;
            this.SrcVar = var;
            this.comboBox1.Items.AddRange(AbstractValueTypeMap.DescTypes);
            
            if (var != null && var.Value != null)
            {
                this.mData = XmlUtil.CloneObject<ZoneVar>(var);
                this.comboBox1.Enabled = false;
            }
            else
            {
                this.mData = new ZoneVar();
                this.mData.Key = varEdit.GenVarKey();
                this.mData.Value = AbstractValueTypeMap.MakeDefault(typeof(IntegerValue));
                if (var != null)
                {
                    this.mData.SyncToClient = var.SyncToClient;
                }
            }
            this.comboBox1.SelectedItem = AbstractValueTypeMap.GetBaseValueType(mData.Value.GetType());
            this.richTextBox1.Text = this.mData.Value.ToString();
            this.textBox1.Text = this.mData.Key;
            this.chk_Sync.Checked = this.mData.SyncToClient;


            this.comboBox1.SelectedIndexChanged +=new EventHandler(comboBox1_SelectedIndexChanged);
            this.textBox1.TextChanged +=new EventHandler(textBox1_TextChanged);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string text = textBox1.Text.Trim();
            this.mData.Key = text;
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TypeDescAttribute baseType = this.comboBox1.SelectedItem as TypeDescAttribute;
            if (baseType != null)
            {
                this.mData.Value = AbstractValueTypeMap.MakeDefault(baseType.DataType);
                this.richTextBox1.Text = this.mData.Value.ToString();
            }
        }
        private void chk_Sync_CheckedChanged(object sender, EventArgs e)
        {
            this.mData.SyncToClient = chk_Sync.Checked;
        }


        private void SceneVarDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                string text = textBox1.Text.Trim();
                if (string.IsNullOrEmpty(text))
                {
                    MessageBox.Show("名字不能为空");
                    e.Cancel = true;
                }
                else if (IsAdd)
                {
                    if (varEdit.ContainsVarKey(Value.Key))
                    {
                        MessageBox.Show("变量名重复");
                        e.Cancel = true;
                    }
                }
                else if (!Value.Key.Equals(SrcVar.Key))
                {
                    if (varEdit.ContainsVarKey(Value.Key))
                    {
                        MessageBox.Show("变量名重复");
                        e.Cancel = true;
                    }
                }
            }
        }

        private void richTextBox1_Click(object sender, EventArgs e)
        {
            TypeDescAttribute baseType = this.comboBox1.SelectedItem as TypeDescAttribute;
            if (baseType != null)
            {
                object result = ValueTypeDialog.ShowEditDialog(this,
                    string.Format("设置 {0} : 初始值", baseType.Desc),
                    baseType.DataType,
                    mData.Value,
                    new SceneDataAdapters(sceneEditor));
                if (result != null)
                {
                    this.mData.Value = result;
                    this.richTextBox1.Text = result.ToString();
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {

        }




    }
}