import inflection from 'inflection'

export default class GqlApi {
  constructor(table, columns, meta, $ctx) {
    this.columns = columns
    this.meta = meta
    this.$ctx = $ctx
  }

  // todo:  - get version letter and use table alias
  async list(params) {
    const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: await this.gqlQuery(params),
      variables: null
    })
    return data.data.data[this.gqlQueryListName]
  }

  async count(params) {
    const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: this.gqlCountQuery(params),
      variables: null
    })
    return data.data.data[this.gqlQueryCountName]
  }

  post(url, params) {
    return this.$axios({
      url: `${url}`,
      method: 'post',
      data: params
    })
  }

  generateQueryParams(params) {
    if (!params) { return '(where:"")' }
    const res = []
    if ('limit' in params) {
      res.push(`limit: ${params.limit}`)
    }
    if ('offset' in params) {
      res.push(`offset: ${params.offset}`)
    }
    if ('where' in params) {
      res.push(`where: ${JSON.stringify(params.where)}`)
    }
    if ('sort' in params) {
      res.push(`sort: ${JSON.stringify(params.sort)}`)
    }
    if (params.condition) {
      res.push(`condition: ${JSON.stringify(params.condition)}`)
    }
    if (params.conditionGraph) {
      res.push(`conditionGraph: ${JSON.stringify(JSON.stringify(params.conditionGraph))}`)
    }
    return `(${res.join(',')})`
  }

  async gqlQuery(params) {
    return `{${this.gqlQueryListName}${this.generateQueryParams(params)}{${this.gqlReqBody}${await this.gqlRelationReqBody(params)}}}`
  }

  gqlReadQuery(id) {
    return `{${this.gqlQueryReadName}(id:"${id}"){${this.gqlReqBody}}}`
  }

  gqlCountQuery(params) {
    return `{${this.gqlQueryCountName}${this.generateQueryParams(params)}}`
  }

  get gqlQueryListName() {
    return `${this.meta.title}List`
  }

  get gqlQueryReadName() {
    return `${this.meta.title}Read`
  }

  get tableCamelized() {
    return `${this.meta.title}`
  }

  get gqlReqBody() {
    return `\n${this.columns.map(c => c.title).join('\n')}\n`
  }

  async gqlRelationReqBody(params) {
    let str = ''
    if (params.hm) {
      for (const child of params.hm.split(',')) {
        await this.$ctx.$store.dispatch('meta/ActLoadMeta', {
          dbAlias: this.$ctx.nodes.dbAlias,
          env: this.$ctx.nodes.env,
          table_name: child
        })
        const meta = this.$ctx.$store.state.meta.metas[child]
        if (meta) {
          str += `\n${meta.title}List{\n${meta.columns.map(c => c.title).join('\n')}\n}`
        }
      }
    }
    if (params.bt) {
      for (const parent of params.bt.split(',')) {
        await this.$ctx.$store.dispatch('meta/ActLoadMeta', {
          dbAlias: this.$ctx.nodes.dbAlias,
          env: this.$ctx.nodes.env,
          table_name: parent
        })
        const meta = this.$ctx.$store.state.meta.metas[parent]
        if (meta) {
          str += `\n${meta.title}Read{\n${meta.columns.map(c => c.title).join('\n')}\n}`
        }
      }
    }
    if (params.mm) {
      for (const mm of params.mm.split(',')) {
        await this.$ctx.$store.dispatch('meta/ActLoadMeta', {
          dbAlias: this.$ctx.nodes.dbAlias,
          env: this.$ctx.nodes.env,
          table_name: mm
        })
        const meta = this.$ctx.$store.state.meta.metas[mm]
        if (meta) {
          str += `\n${meta.title}MMList{\n${meta.columns.map(c => c.title).join('\n')}\n}`
        }
      }
    }
    return str
  }

  get gqlQueryCountName() {
    return `${this.tableCamelized}Count`
  }

  get gqlMutationCreateName() {
    return `${this.tableCamelized}Create`
  }

  get gqlMutationUpdateName() {
    return `${this.tableCamelized}Update`
  }

  get gqlMutationDeleteName() {
    return `${this.tableCamelized}Delete`
  }

  async paginatedList(params) {
    // const list = await this.list(params);
    // const count = (await this.count({where: params.where || ''}));
    const [list, count] = await Promise.all([
      this.list(params), this.count({
        where: params.where || '',
        conditionGraph: params.conditionGraph,
        condition: params.condition
      })
    ])
    return { list, count }
  }

  async update(id, data, oldData) {
    const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: `mutation update($id:String!, $data:${this.tableCamelized}Input){
         ${this.gqlMutationUpdateName}(id: $id, data: $data)
      }`,
      variables: {
        id, data
      }
    })

    const colName = Object.keys(data)[0]
    this.$ctx.$store.dispatch('sqlMgr/ActSqlOp', [{ dbAlias: this.$ctx.nodes.dbAlias }, 'xcAuditCreate', {
      table_name: this.table,
      cn: colName,
      pk: id,
      value: data[colName],
      prevValue: oldData[colName]
    }])

    return data1.data.data[this.gqlMutationUpdateName]
  }

  async insert(data) {
    const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: `mutation create($data:${this.tableCamelized}Input){
         ${this.gqlMutationCreateName}(data: $data){${this.gqlReqBody}}
      }`,
      variables: {
        data
      }
    })
    return data1.data.data[this.gqlMutationCreateName]
  }

  async delete(id) {
    const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: `mutation delete($id:String!){
         ${this.gqlMutationDeleteName}(id: $id)
      }`,
      variables: { id }
    })

    return data1.data.data[this.gqlMutationDeleteName]
  }

  async read(id) {
    const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: this.gqlReadQuery(id),
      variables: null
    })
    return data.data.data[this.gqlQueryReadName]
  }

  get $axios() {
    return this.$ctx.$axios
  }

  get table() {
    return this.meta && this.meta.title && inflection.camelize(this.meta.title)
  }

  async paginatedM2mNotChildrenList(params, assoc, pid) {
    const list = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: `query m2mNotChildren($pid: String!,$assoc:String!,$parent:String!, $limit:Int, $offset:Int){
           m2mNotChildren(pid: $pid,assoc:$assoc,parent:$parent,limit:$limit, offset:$offset)
      }`,
      variables: {
        parent: this.meta.table_name, assoc, pid: pid + '', ...params
      }
    })
    const count = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
      query: `query m2mNotChildrenCount($pid: String!,$assoc:String!,$parent:String!){
           m2mNotChildrenCount(pid: $pid,assoc:$assoc,parent:$parent)
      }`,
      variables: {
        parent: this.meta.table_name, assoc, pid: pid + ''
      }
    })
    return { list: list.data.data.m2mNotChildren, count: count.data.data.m2mNotChildrenCount.count }
  }
}
/**
 * @copyright Copyright (c) 2021, Xgene Cloud Ltd
 *
 * @author Naveen MR <oof1lab@gmail.com>
 * @author Pranav C Balan <pranavxc@gmail.com>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
