import { relationalStore, ValuesBucket } from '@kit.ArkData'

// 图片和视频文件关键信息。(PhotoKeys)
export interface BigWheel extends ValuesBucket {
  id: number | null // 新增时 id 为 null ，自动生成自增 id
  title: string //
  color: string //
  angle: number //
  rotate: number //
  angleStart:number
  angleEnd:number
  proportion:number
}

class BigWheelDB {
  private store: relationalStore.RdbStore | null = null
  private tableName = 'BigWheel'
  // 创建数据库的语句
  private sqlCreate = `CREATE TABLE IF NOT EXISTS ${this.tableName} (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT,
        color TEXT,
        angle INTEGER,
        rotate INTEGER,
        angleStart:INTEGER
        angleEnd:INTEGER
        proportion:INTEGER
      )`

  async getStoreInstance() {
    // 如果数据库已存在，直接退出
    if (this.store) {
      return this.store
    }
    // 存储 store 方便下次直接获取
    this.store = await relationalStore.getRdbStore(getContext(), {
      name: this.tableName + '.db', // 数据库名称
      securityLevel: relationalStore.SecurityLevel.S4 // 安全等级
    })
    this.store.executeSql(this.sqlCreate)
    // 返回 store 实例
    return this.store
  }

  // 新增
  async insert(value: BigWheel) {
    const store = await this.getStoreInstance()
    const rowId = await store.insert(this.tableName, value)
    return rowId > -1 ? Promise.resolve(rowId) : Promise.reject('insert error')
  }

  // 批量新增
  async batchInsert(values: BigWheel[]) {
    const store = await this.getStoreInstance()
    const rowId = await store.batchInsert(this.tableName, values)
    return rowId ? Promise.resolve(rowId) : Promise.reject('batchInsert error')
  }

  // 更新
  async update(item: BigWheel) {
    if (!item.id) {
      return Promise.reject('id error')
    }
    const store = await this.getStoreInstance()
    const predicates = new relationalStore.RdbPredicates(this.tableName)
    predicates.equalTo('id', item.id)
    const rowCount = await store.update(item, predicates)
    return rowCount ? Promise.resolve(rowCount) : Promise.reject('update error')
  }

  // 删除
  async delete(id: number) {
    const store = await this.getStoreInstance()
    const predicates = new relationalStore.RdbPredicates(this.tableName)
    predicates.equalTo('id', id)
    const rowCount = await store.delete(predicates)
    return rowCount ? Promise.resolve(rowCount) : Promise.reject('delete error')
  }

  // 批量删除
  async batchDelete(ids: number[]) {
    const store = await this.getStoreInstance()
    const predicates = new relationalStore.RdbPredicates(this.tableName)
    predicates.in('id', ids)
    const rowCount = await store.delete(predicates)
    return rowCount ? Promise.resolve(rowCount) : Promise.reject('delete error')
  }

  // 查询
  async query(id?: number) {
    const store = await this.getStoreInstance()
    const predicates = new relationalStore.RdbPredicates(this.tableName)
    // 如果有 id
    if (id) {
      predicates.equalTo('id', id)
    }
    const resultSet = await store.query(predicates)
    const list: BigWheel[] = []
    while (resultSet.goToNextRow()) {
      // 获取数据
      const data: BigWheel = {
        id: resultSet.getLong(resultSet.getColumnIndex('id')),
        title: resultSet.getString(resultSet.getColumnIndex('title')),
        color: resultSet.getString(resultSet.getColumnIndex('color')),
        angle: resultSet.getLong(resultSet.getColumnIndex('angle')),
        rotate: resultSet.getLong(resultSet.getColumnIndex('rotate')),
        angleStart: resultSet.getLong(resultSet.getColumnIndex('angleStart')),
        angleEnd: resultSet.getLong(resultSet.getColumnIndex('angleEnd')),
        proportion: resultSet.getLong(resultSet.getColumnIndex('proportion')),
      }
      // 追加到数组中
      list.push(data)
    }
    // 释放资源
    resultSet.close()
    return list
  }

  // 查询数量
  async queryCount() {
    const store = await this.getStoreInstance()
    const predicates = new relationalStore.RdbPredicates(this.tableName)
    const resultSet = await store.query(predicates)
    return resultSet.rowCount > 0 ? resultSet.rowCount : 0
  }
}
export const bigWheelDB = new BigWheelDB()