<?php

namespace OfficeBrain\Bundle\QueueBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your app/config files
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('office_brain_queue');
        $rootNode
            ->children()
                ->scalarNode('server_type')->end()
                ->scalarNode('integrate')->end()
                ->scalarNode('transaction_document')->end()
                ->scalarNode('product_document')->end()
                ->scalarNode('distributor_document')->end()
                ->arrayNode('api_entity_parameters')
                    ->prototype('array')
                        ->children()
                            ->scalarNode('queueClass')->end()
                            ->scalarNode('bundle')->end()                            
                            ->scalarNode('manager')->end()
                            ->scalarNode('noErpField')->end()
                            ->scalarNode('repository')->end()
                            ->variableNode('noErpFieldData')
                                ->validate()
                                ->always(function ($v) {
                                    if (is_string($v) || is_array($v)) {
                                        return $v;
                                    }
                                    throw new InvalidTypeException();
                                })
                                ->end()
                            ->end()
                            ->arrayNode('create')
                                ->children()
                                    ->arrayNode('formType')
                                        ->children()
                                            ->scalarNode('name')->end()
                                            ->variableNode('methods')
                                                ->validate()
                                                ->always(function ($v) {
                                                    if (is_string($v) || is_array($v)) {
                                                        return $v;
                                                    }
                                                    throw new InvalidTypeException();
                                                })
                                                ->end()
                                            ->end()
                                            ->variableNode('parameters')
                                                ->validate()
                                                ->always(function ($v) {
                                                    if (is_string($v) || is_array($v)) {
                                                        return $v;
                                                    }
                                                    throw new InvalidTypeException();
                                                })
                                                ->end()
                                            ->end()
                                            ->variableNode('checkFields')
                                                ->validate()
                                                ->always(function ($v) {
                                                    if (is_string($v) || is_array($v)) {
                                                        return $v;
                                                    }
                                                    throw new InvalidTypeException();
                                                })
                                                ->end()
                                            ->end()
                                        ->end()
                                    ->end()
                                    ->scalarNode('method')->end()
                                    ->variableNode('methodParameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->scalarNode('service')->end()
                                    ->scalarNode('entity')->end()
                                    ->variableNode('conditionalFields')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('formEntities')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('parameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('relationalFields')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('mainResultField')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('afterMethods')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                     ->variableNode('beforeMethods')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->scalarNode('beforeResultUse')->end()
                                    ->variableNode('addFields')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->arrayNode('ignoreFields')
                                        ->prototype('scalar')->end()
                                    ->end()
                                ->end()
                            ->end()
                            ->arrayNode('update')
                                ->children()
                                    ->arrayNode('formType')
                                        ->children()
                                            ->scalarNode('name')->end()
                                            ->variableNode('methods')
                                                ->validate()
                                                ->always(function ($v) {
                                                    if (is_string($v) || is_array($v)) {
                                                        return $v;
                                                    }
                                                    throw new InvalidTypeException();
                                                })
                                                ->end()
                                            ->end()
                                            ->variableNode('parameters')
                                                ->validate()
                                                ->always(function ($v) {
                                                    if (is_string($v) || is_array($v)) {
                                                        return $v;
                                                    }
                                                    throw new InvalidTypeException();
                                                })
                                                ->end()
                                            ->end()
                                            ->variableNode('checkFields')
                                                ->validate()
                                                ->always(function ($v) {
                                                    if (is_string($v) || is_array($v)) {
                                                        return $v;
                                                    }
                                                    throw new InvalidTypeException();
                                                })
                                                ->end()
                                            ->end()
                                        ->end()
                                    ->end()
                                    ->scalarNode('method')->end()
                                    ->variableNode('methodParameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->scalarNode('service')->end()
                                    ->scalarNode('entity')->end()
                                    ->variableNode('conditionalFields')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('formEntities')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('parameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('relationalFields')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                     ->variableNode('beforeMethods')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->scalarNode('beforeResultUse')->end()
                                    ->variableNode('afterMethods')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('addFields')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->arrayNode('ignoreFields')
                                        ->prototype('scalar')->end()
                                    ->end()
                                ->end()
                            ->end()
                            ->arrayNode('remove')
                                ->children()
                                    ->scalarNode('method')->end()
                                    ->scalarNode('service')->end()
                                    ->variableNode('parameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('methodParameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                ->end()
                            ->end()
                            ->arrayNode('updateStatus')
                                ->children()
                                    ->scalarNode('method')->end()
                                    ->scalarNode('service')->end()
                                    ->variableNode('parameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                    ->variableNode('methodParameters')
                                        ->validate()
                                        ->always(function ($v) {
                                            if (is_string($v) || is_array($v)) {
                                                return $v;
                                            }
                                            throw new InvalidTypeException();
                                        })
                                        ->end()
                                    ->end()
                                ->end()
                            ->end()
                            ->variableNode('dataMapping')
                                ->validate()
                                ->always(function ($v) {
                                    if (is_string($v) || is_array($v)) {
                                        return $v;
                                    }
                                    throw new InvalidTypeException();
                                })
                                ->end()
                            ->end()
                            ->variableNode('queueDataMapping')
                                ->validate()
                                ->always(function ($v) {
                                    if (is_string($v) || is_array($v)) {
                                        return $v;
                                    }
                                    throw new InvalidTypeException();
                                })
                                ->end()
                            ->end()
                            ->variableNode('addInCountry')
                                ->validate()
                                ->always(function ($v) {
                                    if (is_string($v) || is_array($v)) {
                                        return $v;
                                    }
                                    throw new InvalidTypeException();
                                })
                                ->end()
                            ->end()
                            ->variableNode('removeFromCountry')
                                ->validate()
                                ->always(function ($v) {
                                    if (is_string($v) || is_array($v)) {
                                        return $v;
                                    }
                                    throw new InvalidTypeException();
                                })
                                ->end()
                            ->end()
                            /*->arrayNode('dataMapping')
                                ->prototype('scalar')->end()
                            ->end()
                            ->arrayNode('queueDataMapping')
                                ->prototype('scalar')->end()
                            ->end()*/
                        ->end()
                    ->end()
                ->end()
                ->scalarNode('document_manager')
                    ->isRequired()
                    ->cannotBeEmpty()
                ->end()
                ->scalarNode('class')
                    ->defaultValue('OfficeBrain\Bundle\QueueBundle\Document\TransactionQueue')
                    ->cannotBeEmpty()
                ->end()
                // ->scalarNode('central_document_manager')
                    // ->isRequired()
                    // ->cannotBeEmpty()
                // ->end()
            ->end()
        ;
        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.

        return $treeBuilder;
    }
}
