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

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

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


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

const ISize = new Schema({
  width: { type: String },
  height: { type: String },
  fit: { type: String },
  gap: { type: String },
});
const IFont = new Schema({
  name: { type: String },
  size: { type: String },
  line_height: { type: Number },
  posiz_text: { type: Number },
  perc_text: { type: String },
});

const IBorders = new Schema({
  top: { type: String },
  bottom: { type: String },
  left: { type: String },
  right: { type: String },
})
const IImg = new Schema({
  imagefile: { type: String },
  fit: { type: String },
})

const IText = new Schema(
  {
    contenuto: String,
    maxlength: Number,
    font: IFont,
    size: ISize,
  }
);

const IDimensioni = new Schema({
  size: ISize,
  margini: IBorders,
  padding: IBorders,
  imgsfondo: IImg,
  text_html: IText,
});
const IPagina = new Schema({
  dimensioni: IDimensioni,
  testo_title: IText,
  testo_up: IText,
  testo_down: IText,
});

const IAreaDiStampa = new Schema({
  margini: IBorders,
  unit: String,
  format: [{ type: Number }],
  format_printable: [{ type: Number }],
  orientation: String,
  compress: Boolean,
  scalex: Number,
  scaley: Number,
  scale_printablex: Number,
  scale_printabley: Number,
  scalecanvas: Number,
});

const INovita = new Schema(
  {
    show: Boolean,
    months: Number,
  }
);

const IBestseller = new Schema(
  {
    show: Boolean,
    quantiFattRaggiunti: Number,
  }
);

const IEtichette = new Schema(
  {
    novita: INovita,
    bestseller: IBestseller,
  }
);

const IBarCode = new Schema(
  {
    show: Boolean,
    format: Number,
    size: ISize,
    font: IFont,
    widthlines: Number,
    show_at_right: Boolean,
  }
);

const IElementiScheda = new Schema({
  pagina: IPagina,
  riga: IDimensioni,
  scheda_prodotto: IDimensioni,
  immagine_prodotto: IDimensioni,
});

const scheletroScheda = {
  idapp: { type: String },
  isTemplate: { type: Boolean },
  isPagIntro: { type: Boolean },
  linkIdTemplate: { type: String },
  scalexscheda: Number,
  scaleyscheda: Number,
  name: { type: String },
  numschede_perRiga: { type: Number },
  numschede_perCol: { type: Number },
  show_separatore: { type: Boolean },
  testo_right_attaccato: IText,
  testo_right: IText,
  testo_bottom: IText,
  barcode: IBarCode,
  etichette: IEtichette,
  
  dimensioni: IElementiScheda,

  productTypes: [{ type: Number }],
  excludeproductTypes: [{ type: Number }],
  idTipologie: [{ type: Number }],
  idTipoFormato: [{ type: Number }],
  editore: [{ type: String }],
  argomenti: [{ type: String }],
  idCollane: [{ type: String }],
  author: { type: String },
  sort_field: { type: String },
  sort_dir: { type: Number },
  arrProdottiSpeciali: [{ type: String }],
};

const MySchedaSchema = new Schema(
  scheletroScheda
);


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

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

MySchedaSchema.statics.findAllIdApp = async function (idapp) {

};

const MyScheda = mongoose.model('MyScheda', MySchedaSchema);

MyScheda.createIndexes()
  .then(() => { })
  .catch((err) => { throw err; });


module.exports = { MyScheda, MySchedaSchema, IDimensioni, IImg, IText, IAreaDiStampa, IImg };
