using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;

namespace Nuvolar.Module.Web.CSharp.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class ActionDefaultPaintStyle_VC : ViewController
    {
        public ActionDefaultPaintStyle_VC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            if (Frame.Template!=null)
            {
                foreach (IActionContainer _IActionContainer in Frame.Template.GetContainers())
                {
                    foreach (ActionBase _ActionBase in _IActionContainer.Actions)
                    {
                        CustomizeAction(_ActionBase);
                    }
                }
            }
            if ((Frame as NestedFrame)!=null)
            {
                NestedFrame _NestedFrame = Frame as NestedFrame;
                if (_NestedFrame.Controllers.Count>0)
                {
                    foreach (Controller _Controller in _NestedFrame.Controllers)
                    {
                        foreach (ActionBase _ActionBase in _Controller.Actions)
                        {
                            CustomizeAction(_ActionBase);
                        }
                    }
                }
            }
            View.ControlsCreated += View_ControlsCreated;
        }

        private void View_ControlsCreated(object sender, EventArgs e)
        {
            if ((View as DetailView)!=null)
            {
                DetailView _DetailView = View as DetailView;
                foreach (ViewItem _ViewItem in _DetailView.Items)
                {
                    if ((_ViewItem as ActionContainerViewItem)!=null)
                    {
                        (_ViewItem as ActionContainerViewItem).ControlCreated += ActionContainerViewItem_ControlCreated;
                    }
                }
            }
        }

        private void ActionContainerViewItem_ControlCreated(object sender, EventArgs e)
        {
            foreach (ActionBase _ActionBase in (sender as ActionContainerViewItem).Actions)
            {
                CustomizeAction(_ActionBase);
            }
        }

        private void CustomizeAction(ActionBase _ActionBase)
        {
            if (_ActionBase.PaintStyle== ActionItemPaintStyle.Default)
            {
                if (_ActionBase.Category!=_ActionBase.Id && (View as DetailView)!=null)
                {
                    _ActionBase.PaintStyle = ActionItemPaintStyle.Image;
                }
                if ((View as ListView)!=null)
                {
                    _ActionBase.PaintStyle = ActionItemPaintStyle.Image;
                }
            }
        }

        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }
    }
}
