import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  OneToMany,
  Timestamp,
  OneToOne,
  JoinColumn,
} from 'typeorm';

import { PersonEntity } from '../user/user.entity';
import { GameGroupEntity } from '../faction/faction.entity';
import { FactionEntity } from '../faction/faction.entity';
import { TaskEntity } from '../task/task.entity';
import { CenterDTO, NodeSettingsDTO } from './game.dto';

// table that stores all created games
@Entity('Game')
export class GameEntity {
  @PrimaryGeneratedColumn('uuid') id: string;
  @Column('text') name: string;
  @Column('text') desc: string;
  @Column('json') center: CenterDTO;
  @Column({ type: 'json', nullable: true }) map: JSON;
  @Column({ type: 'json', nullable: true }) nodesettings?: NodeSettingsDTO;
  @Column('text') state: string;
  @Column('timestamp') startdate: Timestamp;
  @Column('timestamp') enddate: Timestamp;
  @Column('text') image: string;

  @OneToMany(type => FactionEntity, factions => factions.game)
  factions: FactionEntity[];
  @OneToMany(type => Game_PersonEntity, game_persons => game_persons.game)
  game_persons: Game_PersonEntity[];
  @OneToMany(
    type => ObjectivePointEntity,
    objective_points => objective_points.game,
  )
  objective_points: ObjectivePointEntity[];
  @OneToMany(type => TaskEntity, tasks => tasks.taskGame)
  tasks: TaskEntity[];

  gameObject() {
    const { id, name } = this;
    return { id, name };
  }
}

// table that stores players associated with particular game
@Entity('Game_Person', {
  orderBy: {
    person: 'ASC',
  },
})
export class Game_PersonEntity {
  @PrimaryGeneratedColumn('uuid') gamepersonId: string;
  @Column({ type: 'text', nullable: true }) role: string;
  // If a Faction or Game where the GamePerson was in is deleted, the GamePerson is also deleted
  @ManyToOne(type => FactionEntity, faction => faction.game_persons, {
    onDelete: 'CASCADE',
  })
  faction: FactionEntity;
  @ManyToOne(type => GameEntity, game => game.id, {
    onDelete: 'CASCADE',
  })
  game: GameEntity;
  @ManyToOne(type => PersonEntity, person => person.id)
  person: PersonEntity;
  @OneToOne(type => GameGroupEntity, group => group.leader)
  leaderGroup: GameGroupEntity;

  // When a Group where GamePerson is is deleted, nothing happens to the GamePerson
  @ManyToOne(type => GameGroupEntity, group => group.players, {
    onDelete: 'NO ACTION',
  })
  @JoinColumn({ name: 'group' })
  group: GameGroupEntity;
}

@Entity('ObjectivePoint')
export class ObjectivePointEntity {
  @PrimaryGeneratedColumn('uuid') objectivePointId: string;
  @Column({ type: 'text' }) objectivePointDescription: string;
  @Column({ type: 'float' }) objectivePointMultiplier: number;
  @Column({ type: 'json' }) data: JSON;

  // If the Game where the ObjectivePoint was in is deleted, the ObjectivePoint is also deleted
  @ManyToOne(type => GameEntity, game => game.objective_points, {
    onDelete: 'CASCADE',
  })
  game: GameEntity;
  @OneToMany(
    () => ObjectivePoint_HistoryEntity,
    history => history.objective_point,
    {
      onDelete: 'NO ACTION',
    },
  )
  history: ObjectivePoint_HistoryEntity[];
}

@Entity('ObjectivePoint_History')
export class ObjectivePoint_HistoryEntity {
  @PrimaryGeneratedColumn('uuid') oP_HistoryId: string;
  @Column({ type: 'float' }) oP_HistoryTimestamp: number;
  @Column('float') action: number;

  // If the owner Faction, capturer Faction or ObjectivePoint, that has, is trying to have or is the point where
  // ObjectivePointHistory points to is deleted, the ObjectivePointHistory is also deleted
  @ManyToOne(type => FactionEntity, factionEntity => factionEntity.factionId, {
    onDelete: 'CASCADE',
  })
  capture: FactionEntity;
  @ManyToOne(type => FactionEntity, factionentity => factionentity.factionId, {
    onDelete: 'CASCADE',
  })
  owner: FactionEntity;
  @ManyToOne(
    type => ObjectivePointEntity,
    objective_point => objective_point.objectivePointId,
    {
      onDelete: 'CASCADE',
    },
  )
  objective_point: string;
}