var Schema = require('mongoose').Schema;
const config = require('../config/app');

let artSchema = new Schema({
  open: { type: Boolean, default: false, index: true, desc: '是否开放' }, // 针对网站新增的字段，控制内容逐步开放

  status: { type: Number, required: true, index: true, orderable: true, default: 1000, desc: '状态' },
  pageId: { type: Schema.Types.ObjectId, required: true, desc: 'Page Id' }, //old modes.Page._id
  user: { type: Schema.Types.ObjectId, ref: 'User', required: true, index: true, desc: '作者' },

  org: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '线稿原图' },
  rawOrg: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '初始线稿原图' },
  raw: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '矢量线稿', },
  rawColored: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '原始上色效果图' },

  page: { type: Schema.Types.ObjectId, ref: 'ArtBin', required: true, desc: '像素线稿', },
  pageVersion: { type: Number, default: 1, desc: '底稿版本', },
  rawMap: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '初始Map', },

  colorUser: { type: Schema.Types.ObjectId, ref: 'User', index: true, desc: '填色人', },

  areaCount: { type: Number, required: true, orderable: true, desc: '区块数' },
  coloredAreaCount: { type: Number, default: 0, desc: '填色区块数' },
  colorCount: { type: Number, default: 0, desc: '颜色数' },
  orderedColorCount: { type: Number, default: 0, desc: '已排序颜色数' },


  upstream: { type: Boolean, default: false, index: true, desc: '是否推送' },
  upstreamTime: { type: Date, index: true, desc: '推送时间', orderable: true, },
  upstreamBy: { type: Schema.Types.ObjectId, ref: 'User', index: true, desc: '推送人' },
  upstreamFrom: { type: String, index: true, desc: '推送来源' },

  work: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '上色效果', },
  map: { type: Schema.Types.ObjectId, ref: 'ArtBin', required: true, desc: 'Map图', },
  centers: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '填色区域中心点', },

  mapVersion: { type: Number, required: true, default: 1, desc: 'map版本', },
  workVersion: { type: Number, required: true, default: 1, desc: 'work版本', },
  centersVersion: { type: Number, required: true, default: 0, desc: '中心点版本', },


  hasSpecial: { type: Boolean, desc: '是否有special', default: false, index: true, },
  special: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: 'Special图', },
  specialHalf: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: 'Special图一半大小', },
  specialVersion: { type: Number, default: 0, desc: 'Special图版本', },
  specialOutline: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: 'Speical切线图', },
  specialThumb: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: 'Special上传缩略图' },
  useSpecialThumb: { type: Number, required: true, default: 0, desc: '当前Special缩略图' },

  mystery: { type: Boolean, default: false, index: true, desc: '神秘图', },
  ai: { type: Boolean, default: false, index: true, desc: '是否AI参考图', },
  aiPrompt: { type: String, desc: 'AI关键字' },
  aiImage: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: 'AI参考图', },


  width: { type: Number, required: true, index: true, desc: '宽' },
  height: { type: Number, index: true, required: true, desc: '高' },
  name: { type: String, required: true, desc: '作品名', searchable: true },
  // 网站seo meta  description， 160个字符以内
  desc: { type: String, desc: '作品描述', searchable: true },  // json字符串，形如： {zh: '中国', en: 'China'}
  // 新增字段，网站title，60个字符以内
  title: { type: String, desc: '作品标题', searchable: true }, // json字符串，形如： {zh: '中国', en: 'China'}
  // 文案描述， 200字左右
  copy: { type: String, desc: '文案描述', searchable: true }, // json字符串，形如： {zh: '中国', en: 'China'}
  use: { type: String, required: true, index: true, default: 'normal', lowercase: true, trim: true, desc: '用途', searchable: true },
  tags: { type: [String], index: true, lowercase: true, trim: true, desc: '标签', searchable: true },

  epgs: { type: [String], index: true, trim: true, desc: 'EPG', searchable: true },  // 绑定的epg列表, 初始由user表带过来, 后期可再编辑变更

  date: { type: Date, default: Date.now, index: true, desc: '上传时间', orderable: true, },
  lastMod: { type: Date, default: Date.now, index: true, desc: '修改时间', orderable: true, },
  timeSubmit: { type: Date, index: true, desc: '提测时间', }, //提测时间
  timeReady: { type: Date, index: true, desc: '通过时间', }, //通过时间

  refuseReason: { type: String, desc: '拒稿原因' },
  mark: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '标注' },  // 拒稿和打回修改共用
  markImg: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '标注图' }, // 拒稿和打回修改共用

  colorMap: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '填色表', },
  colorSum: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '颜色统计', },
  colorOrder: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '填色顺序', },
  orderAuto: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '自动顺序', },

  svgv1: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '全矢量化v1', },
  svgv2: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '全矢量化v2', },
  svgall: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '全矢量化all', },

  numberConfig: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '填色标签配置', },
  numberConfigv2: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: '填色标签配置V2', },

  openglMap: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: 'opengl专属Map图', },
  config: { type: Schema.Types.ObjectId, ref: 'ArtBin', desc: 'config.json', }, // opengl配置

  lock: { type: Boolean, default: false, index: true, desc: '是否上锁', },
  publishVersion: { type: Number, default: 0, index: true, desc: '发布版本' },
  publishTime: { type: Date, index: true, desc: '发布时间', orderable: true, },
  publishBy: { type: Schema.Types.ObjectId, ref: 'User', index: true, desc: '发布者' },
  order: { type: Number, default: 0, index: true, },


  drop: { type: Boolean, default: false, index: true, desc: '标记删除' },
  dropReason: { type: String, desc: '删除原因', },
  dropBy: { type: Schema.Types.ObjectId, ref: 'User', index: true, desc: '谁标记删除的' },

  publishSchedule: { type: Schema.Types.ObjectId, ref: 'PublishSchedule', index: true, desc: '定时发布' },
  scheduleToEpg: { type: [String], default: [], desc: '上架计划' },  // 格式： epgId->columnId

  score: { type: Number, orderable: true, desc: '评分' }, // 总体评分0-5

  //数据库内不存储
  thumb: String,
  //zip: String,

}, {
  strict: true,
  toJSON: {
    virtuals: true,
    transform: artTransform,
  },
  toObject: {
    virtuals: true,
    transform: artTransform,
  },
});


function artTransform(doc, ret) {
  //make thumb for art.
  ret.thumb = `${config.resHost}/thumbs/coloring-page/page/320/${doc.pageId}.png?t=${doc.lastMod.getTime()}`;
  if (doc.work && doc.hasSpecial) {
    ret.thumb = `${config.resHost}/thumbs/coloring-page/special/320/${doc._id}.png?t=${doc.lastMod.getTime()}`;
  } else if (doc.work) {
    ret.thumb = `${config.resHost}/thumbs/coloring-page/work/320/${doc._id}.png?t=${doc.lastMod.getTime()}`
  }

  ret.binHash = {
    "page": `${config.resHost}/thumbs/coloring-page/page/640/${doc._id}.png?t=${doc.lastMod.getTime()}`,
    "work": doc.work ? `${config.resHost}/thumbs/coloring-page/work/640/${doc._id}.png?t=${doc.lastMod.getTime()}` : undefined,
    "special": doc.special ? `${config.resHost}/thumbs/coloring-page/special/640/${doc._id}.png?t=${doc.lastMod.getTime()}` : undefined,
    "specialThumb": doc.specialThumb ? `${config.resHost}/thumbs/coloring-page/special_thumb/640/${doc._id}.png?t=${doc.lastMod.getTime()}` : undefined,
    "specialOutline": doc.specialOutline ? `${config.resHost}/thumbs/coloring-page/special_outline/640/${doc._id}.png?t=${doc.lastMod.getTime()}` : undefined,
    "aiImage": doc.aiImage ? `${config.resHost}/thumbs/coloring-page/ai_image/640/${doc._id}.png?t=${doc.lastMod.getTime()}` : undefined,
  }
}

artSchema.index({
  order: -1,
  publishTime: -1
});



module.exports = artSchema;
