const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;

const tools = require('../tools/general');
const { ObjectID, ObjectId } = require('mongodb');

mongoose.Promise = global.Promise;
mongoose.level = "F";


// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
  schema.options.usePushEach = true
});

const myCard = new Schema(
  {
    imagefile: String,
    alt: String,
    description: String,
    style: String,
    size: String,
    color: String,
    content: String,
    colorsub: String,
  }
)
const animation = new Schema(
  {
    name: String,
    clduration: String,
    cldelay: String,
    timingtype: String,
  }
);

const elemText = new Schema(
  {
    text: String,
    color: String,
    class: String,
    size: String,
    anim: animation,
  }
);

const MyElemSchema = new Schema({
  _id: {
    type: ObjectId,
    default: function () {
      return new ObjectId();
    },
  },
  idapp: {
    type: String,
  },
  path: {
    type: String,
  },
  type: {
    type: Number,
  },
  img: {
    type: String,
  },
  container: {
    type: String,
  },
  container2: {
    type: String,
  },
  container3: {
    type: String,
  },
  container4: {
    type: String,
  },
  align: {
    type: Number,
  },
  vertalign: {
    type: Number,
  },
  speed: {
    type: Number,
  },
  parambool: {
    type: Boolean,
  },
  span: {
    type: Boolean,
  },
  parambool2: {
    type: Boolean,
  },
  parambool3: {
    type: Boolean,
  },
  number: {
    type: String,
  },
  imgback: {
    type: String,
  },
  ratio: {
    type: String,
  },
  containerHtml: {
    type: String,
  },
  size: {
    type: String,
  },
  order: {
    type: Number,
    default: 0,
  },
  height: {
    type: Number,
  },
  heightimg: {
    type: String,
  },
  widthimg: {
    type: String,
  },
  width: {
    type: Number,
  },
  link: {
    type: String,
  },
  fit: {
    type: String,
  },
  onlyif_logged: {
    type: Boolean,
  },
  color: {
    type: String,
  },
  elemsText: [elemText],
  anim: animation,
  active: {
    type: Boolean,
    default: false,
  },
  class: {
    type: String,
  },
  class2: {
    type: String,
  },
  class3: {
    type: String,
  },
  class4: {
    type: String,
  },
  styleadd: {
    type: String,
  },
  image: {
    type: String,
  },
  listcards: [myCard],
  list: [
    {
      imagefile: {
        type: String
      },
      order: {
        type: Number
      },
      alt: {
        type: String
      },
      description: {
        type: String
      }
    }
  ],
});

MyElemSchema.pre('save', async function (next) {
  if (this.isNew) {
    this._id = new ObjectID();
  }

  next();
});

MyElemSchema.statics.getFieldsForSearch = function () {
  return [{ field: 'title', type: tools.FieldType.string },
  { field: 'content', type: tools.FieldType.string }]
};

MyElemSchema.statics.executeQueryTable = function (idapp, params, user) {
  params.fieldsearch = this.getFieldsForSearch();
  return tools.executeQueryTable(this, idapp, params, user);
};

MyElemSchema.statics.findAllIdApp = async function (idapp) {
  const MyElem = this;

  const myfind = { idapp };

  return await MyElem.find(myfind).sort({ order: 1 });
};

const MyElem = mongoose.model('MyElem', MyElemSchema);

module.exports = { MyElem };
