//
// Copyright © 2020 Anticrm Platform Contributors.
//
// Licensed under the Eclipse Public 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 https://www.eclipse.org/legal/epl-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.
//

import type { Plugin } from '@anticrm/platform'
import { plugin } from '@anticrm/platform'
import type { Class, Data, Doc, Obj, Ref, Mixin, Arr } from '../classes'
import { ClassifierKind, DOMAIN_MODEL } from '../classes'
import type { TxCUD, TxCreateDoc } from '../tx'
import core from '../component'
import { DOMAIN_TX, TxFactory } from '../tx'

const txFactory = new TxFactory(core.account.System)

function createClass (_class: Ref<Class<Obj>>, attributes: Data<Class<Obj>>): TxCreateDoc<Doc> {
  return txFactory.createTxCreateDoc(core.class.Class, core.space.Model, attributes, _class)
}

export function createDoc<T extends Doc> (_class: Ref<Class<T>>, attributes: Data<T>): TxCreateDoc<Doc> {
  return txFactory.createTxCreateDoc(_class, core.space.Model, attributes)
}

export interface TestMixin extends Doc {
  arr: Arr<string>
}

export const test = plugin('test' as Plugin, {
  mixin: {
    TestMixin: '' as Ref<Mixin<TestMixin>>
  }
})

/**
 * Generate minimal model for testing purposes.
 * @returns R
 */
export function genMinModel (): TxCUD<Doc>[] {
  const txes = []
  // Fill Tx'es with basic model classes.
  txes.push(createClass(core.class.Obj, { kind: ClassifierKind.CLASS }))
  txes.push(createClass(core.class.Doc, { extends: core.class.Obj, kind: ClassifierKind.CLASS }))
  txes.push(createClass(core.class.Class, { extends: core.class.Doc, kind: ClassifierKind.CLASS, domain: DOMAIN_MODEL }))
  txes.push(createClass(core.class.Space, { extends: core.class.Doc, kind: ClassifierKind.CLASS, domain: DOMAIN_MODEL }))
  txes.push(createClass(core.class.Account, { extends: core.class.Doc, kind: ClassifierKind.CLASS, domain: DOMAIN_MODEL }))

  txes.push(createClass(core.class.Tx, { extends: core.class.Doc, kind: ClassifierKind.CLASS, domain: DOMAIN_TX }))
  txes.push(createClass(core.class.TxCreateDoc, { extends: core.class.Tx, kind: ClassifierKind.CLASS }))
  txes.push(createClass(core.class.TxUpdateDoc, { extends: core.class.Tx, kind: ClassifierKind.CLASS }))
  txes.push(createClass(core.class.TxRemoveDoc, { extends: core.class.Tx, kind: ClassifierKind.CLASS }))

  txes.push(createClass(test.mixin.TestMixin, { extends: core.class.Doc, kind: ClassifierKind.MIXIN }))

  txes.push(
    createDoc(core.class.Space, {
      name: 'Sp1',
      description: '',
      private: false,
      members: []
    })
  )

  txes.push(
    createDoc(core.class.Space, {
      name: 'Sp2',
      description: '',
      private: false,
      members: []
    })
  )
  return txes
}
