import EventEmitter from "events";
import { PlayObject, SOURCE_SOT } from "../../core/Atomic.js";
import { isNodeNetworkException } from "../common/errors/NodeErrors.js";
import { FormatPlayObjectOptions, InternalConfig } from "../common/infrastructure/Atomic.js";
import { RecentlyPlayedOptions } from "./AbstractSource.js";
import MemorySource from "./MemorySource.js";
import { AbstractBlueSkyApiClient, listRecordToPlay, recordToPlay } from "../common/vendor/bluesky/AbstractBlueSkyApiClient.js";
import { TealSourceConfig } from "../common/infrastructure/config/source/tealfm.js";
import { BlueSkyAppApiClient } from "../common/vendor/bluesky/BlueSkyAppApiClient.js";
import { BlueSkyOauthApiClient } from "../common/vendor/bluesky/BlueSkyOauthApiClient.js";
import { parseArrayFromMaybeString } from "../utils/StringUtils.js";

export default class TealfmSource extends MemorySource {

    client: AbstractBlueSkyApiClient;
    requiresAuth = true;
    requiresAuthInteraction = false;

    serviceAllow: string[]
    serviceDeny: string[]

    declare config: TealSourceConfig;

    constructor(name: any, config: TealSourceConfig, internal: InternalConfig, emitter: EventEmitter) {
        const {
            data: {
                interval = 15,
                maxInterval = 60,
                ...restData
            } = {}
        } = config;
        super('tealfm', name, {...config, data: {interval, maxInterval, ...restData}}, internal, emitter);
        this.canPoll = true;
        this.canBacklog = true;
        if(config.data.appPassword !== undefined) {
            this.client = new BlueSkyAppApiClient(name, config.data, {...internal, logger: internal.logger});
            this.requiresAuthInteraction = false;
        } else if(config.data.baseUri !== undefined) {
            this.client = new BlueSkyOauthApiClient(name, config.data, {...internal, logger: internal.logger});
        } else {
            throw new Error(`Must define either 'baseUri' or 'appPassword' in configuration!`);
        }
        this.playerSourceOfTruth = SOURCE_SOT.HISTORY;
        this.supportsUpstreamRecentlyPlayed = true
        this.SCROBBLE_BACKLOG_COUNT = 20;
    }

    static formatPlayObj(obj: any, options: FormatPlayObjectOptions = {}){ return recordToPlay(obj); }

    protected async doBuildInitData(): Promise<true | string | undefined> {
        const {
            data: {
                identifier,
                serviceAllow = [],
                serviceDeny = []
            } = {}
        } = this.config;
        if (identifier === undefined) {
            throw new Error('Must provide an identifier');
        }

        await this.client.initClient();

        this.serviceAllow = parseArrayFromMaybeString(serviceAllow, {lower: true});
        if(this.serviceAllow.length > 0) {
            this.logger.debug(`Discover Plays from these services only: ${this.serviceAllow.join(',')}`);
        } else {
            this.serviceDeny = parseArrayFromMaybeString(serviceDeny, {lower: true});
            if(this.serviceDeny.length > 0) {
                this.logger.debug(`Do not discover Plays from these services: ${this.serviceDeny.join(',')}`);
            }
        }

        return true;
    }

    protected async doCheckConnection(): Promise<true | string | undefined> {
        if (this.client instanceof BlueSkyAppApiClient) {
            try {
                return await this.client.checkPds();
            } catch (e) {
                throw e;
            }
        } else {
            return true;
        }
    }

    doAuthentication = async () => {
        try {
            const sessionRes = await this.client.restoreSession();
            if(sessionRes) {
                return true;
            }
            if(this.client instanceof BlueSkyAppApiClient) {
                return await this.client.appLogin();
            }
        } catch (e) {
            if(isNodeNetworkException(e)) {
                this.logger.error('Could not communicate with ATProto API');
            }
            throw e;
        }
    }


    getRecentlyPlayed = async(options: RecentlyPlayedOptions = {}) => {
        const {limit = 20} = options;
        let plays: PlayObject[];
        try {
            const {data} = await this.client.getPagelessTimeRangeListens({limit})
            plays = data;
        } catch (e) {
            throw new Error('Error occurred while trying to fetch records', {cause: e});
        }
        await this.processRecentPlays([]);
        if(this.serviceAllow.length > 0) {
            plays = plays.filter(x => 
                (x.meta.musicService !== undefined && this.serviceAllow.some(y => x.meta.musicService.toLocaleLowerCase().includes(y)))
            || (x.meta.musicService === undefined && this.serviceAllow.includes('unknown'))
        );
        } else if(this.serviceDeny.length > 0) {
            plays = plays.filter(x => 
                (x.meta.musicService !== undefined && !this.serviceDeny.some(y => x.meta.musicService.toLocaleLowerCase().includes(y)))
                || (x.meta.musicService === undefined && !this.serviceAllow.includes('unknown'))
            );
        }
        return plays;
    }

    getUpstreamRecentlyPlayed = async (options: RecentlyPlayedOptions = {}): Promise<PlayObject[]> => {
        try {
            return await this.getRecentlyPlayed(options);
        } catch (e) {
            throw e;
        }
    }

    protected getBackloggedPlays = async (options: RecentlyPlayedOptions = {}) =>  await this.getRecentlyPlayed({formatted: true, ...options})
}
