/*
 * Copyright © 2016 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

angular.module(`${PKG.name}.feature.experimental`)
  .controller('ComplexSchemaController', function(avsc) {
    var schema = '{"type":"record","name":"rec","fields":[{"name":"bool_field","type":["boolean","null"]},{"name":"int_field","type":"int"},{"name":"long_field","type":"long"},{"name":"float_field","type":"float"},{"name":"double_field","type":"double"},{"name":"bytes_field","type":"bytes"},{"name":"array_field","type":{"type":"array","items":["string","null"]}},{"name":"enum_field","type":{"type":"enum","symbols":["Monday","Tuesday","Wednesday"]}},{"name":"union_field","type":["double","float","bytes"]},{"name":"map_field","type":{"type":"map","keys":["string","null"],"values":["int","null"]}},{"name":"record_field","type":[{"type":"record","name":"rec1","fields":[{"name":"x","type":["int","null"]},{"name":"y","type":["double","null"]}]},"null"]},{"name":"string_field","type":["string","null"]}]}';


    // let union = {
    //   name: 'test',
    //   type: 'record',
    //   fields: [
    //     {
    //       name: 'string_field',
    //       type: ['float', 'int']
    //     }
    //   ]
    // };
    // let parsed = avsc.parse(union, { wrapUnions: true });
    // console.log('parsed', parsed);


    let parsed = avsc.parse(schema, { wrapUnions: true }).getFields();

    angular.forEach(parsed, (field) => {
      let name = field.getName();
      let type = field.getType();

      if (type.getTypeName() === 'union:wrapped') {
        let temp = '';

        type = type.getTypes();
        angular.forEach(type, (tempType) => {
          temp = temp + tempType.getTypeName() + ' ';
        });
        type = temp;

      } else {
        type = type.getTypeName();
      }

      console.log(name + ': ' + type);
    });

    // function parseSchemaType (field) {

    // }


  });

@@ -1819,16 +1819,229 @@
 rsed);%0A%0A
+    const SCHEMA_TYPES = %5B%0A      'string',%0A      'int',%0A      'long',%0A      'float',%0A      'double',%0A      'boolean',%0A      'bytes',%0A      'enum',%0A      'array',%0A      'map',%0A      'record',%0A      'union'%0A    %5D;%0A%0A
 %0A    let
@@ -2554,16 +2554,50 @@
 + type);
+%0A%0A      console.log(SCHEMA_TYPES);
 %0A    %7D);

add schema types array