import { Connection } from 'typeorm';
import { Factory, Seeder } from 'typeorm-seeding';
import { CreateGroupContact } from '../models/actions/create/CreateGroupContact';
import { CreateRunner } from '../models/actions/create/CreateRunner';
import { CreateRunnerOrganization } from '../models/actions/create/CreateRunnerOrganization';
import { CreateRunnerTeam } from '../models/actions/create/CreateRunnerTeam';
import { Address } from '../models/entities/Address';
import { GroupContact } from '../models/entities/GroupContact';
import { Runner } from '../models/entities/Runner';
import { RunnerGroup } from '../models/entities/RunnerGroup';
import { RunnerOrganization } from '../models/entities/RunnerOrganization';
import { RunnerTeam } from '../models/entities/RunnerTeam';

/**
 * Seeds a test runner org with a test runner team ans some test runners.
 * Usefull for testing or demo instances.
 */
export default class SeedTestRunners implements Seeder {
    public async run(factory: Factory, connection: Connection): Promise<any> {
        let testOrg: RunnerOrganization = await this.createTestOrg(connection);
        let testTeam: RunnerTeam = await this.createTestTeam(connection, testOrg);
        await this.createTestContact(connection, testOrg);
        await this.createTestRunners(connection, testOrg);
        await this.createTestRunners(connection, testTeam);
    }

    public async createTestOrg(connection: Connection): Promise<RunnerOrganization> {
        let testOrg = new CreateRunnerOrganization();
        testOrg.name = "Test Org";

        testOrg.address = new Address();
        testOrg.address.address1 = "Test street 1";
        testOrg.address.city = "Herzogenaurach";
        testOrg.address.country = "Germany";
        testOrg.address.postalcode = "90174";

        return await connection.getRepository(RunnerOrganization).save(await testOrg.toEntity());
    }

    public async createTestTeam(connection: Connection, org: RunnerOrganization): Promise<RunnerTeam> {
        let testTeam = new CreateRunnerTeam();
        testTeam.name = "Test Team";
        testTeam.parentGroup = org.id;

        return await connection.getRepository(RunnerTeam).save(await testTeam.toEntity());
    }

    public async createTestRunners(connection: Connection, group: RunnerGroup) {
        for (let first of this.firstnames) {
            for (let last of this.lastnames) {
                let runner = new CreateRunner;
                runner.firstname = first;
                runner.lastname = last;
                runner.middlename = group.name;
                runner.group = group.id;
                await connection.getRepository(Runner).save(await runner.toEntity());
            }
        }
    }

    public async createTestContact(connection: Connection, group: RunnerGroup) {
        let contact = new CreateGroupContact;
        contact.firstname = "Test";
        contact.lastname = "Contact";
        contact.email = "test.contact@dev.lauf-fuer-kaya.de";
        contact.groups = group.id;

        contact.address = new Address();
        contact.address.address1 = "First Contact Street 100";
        contact.address.city = "Herzogenaurach";
        contact.address.country = "Germany";
        contact.address.postalcode = "90174";

        await connection.getRepository(GroupContact).save(await contact.toEntity());
    }

    private firstnames = [
        "Peter",
        "Matze",
        "Tine",
        "Uta",
        "Fabian",
        "Unicode:ÖÄ?✔⚠"
    ]

    private lastnames = [
        "Muster",
        "Example",
        "Müller",
        "Unicode:搆Ǩ>ÙՠƳ|"
    ]

}