import { OrderedSet } from 'molstar/lib/mol-data/int';
import { AfConfidence, AfConfidenceProvider } from './prop';
import { AfConfidenceColorThemeProvider } from './color';
import { Loci } from 'molstar/lib/mol-model/loci';
import { StructureElement } from 'molstar/lib/mol-model/structure';
import { ParamDefinition as PD } from 'molstar/lib/mol-util/param-definition';
import { PluginBehavior } from 'molstar/lib/mol-plugin/behavior/behavior';

export const AfConfidenceScore = PluginBehavior.create<{ autoAttach: boolean, showTooltip: boolean }>({
    name: 'af-confidence-prop',
    category: 'custom-props',
    display: {
        name: 'AF Confidence Score',
        description: 'AF Confidence Score.'
    },
    ctor: class extends PluginBehavior.Handler<{ autoAttach: boolean, showTooltip: boolean }> {

        private provider = AfConfidenceProvider

        private labelAfConfScore = {
            label: (loci: Loci): string | undefined => {
                console.log(`AF label`);
                if (!this.params.showTooltip) return void 0;

                switch (loci.kind) {
                    case 'element-loci':
                        if (loci.elements.length === 0) return void 0;
                        const e = loci.elements[0];
                        const u = e.unit;
                        if (!u.model.customProperties.hasReference(AfConfidenceProvider.descriptor)) return void 0;

                        const se = StructureElement.Location.create(loci.structure, u, u.elements[OrderedSet.getAt(e.indices, 0)]);
                        const confidenceScore = AfConfidence.getConfidenceScore(se);
                        return confidenceScore ? `Confidence score: ${confidenceScore[0]} <small>( ${confidenceScore[1]} )</small>` : `No confidence score`;

                    default: return void 0;
                }
            }
        }

        register(): void {
            console.log(`AF register`);
            this.ctx.customModelProperties.register(this.provider, this.params.autoAttach);
            this.ctx.managers.lociLabels.addProvider(this.labelAfConfScore);

            this.ctx.representation.structure.themes.colorThemeRegistry.add(AfConfidenceColorThemeProvider);
        }

        update(p: { autoAttach: boolean, showTooltip: boolean }) {
            console.log(`AF update`);
            let updated = this.params.autoAttach !== p.autoAttach;
            this.params.autoAttach = p.autoAttach;
            this.params.showTooltip = p.showTooltip;
            this.ctx.customModelProperties.setDefaultAutoAttach(this.provider.descriptor.name, this.params.autoAttach);
            return updated;
        }

        unregister() {
            console.log(`AF unregister`);
            this.ctx.customModelProperties.unregister(AfConfidenceProvider.descriptor.name);
            this.ctx.managers.lociLabels.removeProvider(this.labelAfConfScore);
            this.ctx.representation.structure.themes.colorThemeRegistry.remove(AfConfidenceColorThemeProvider);
        }
    },
    params: () => ({
        autoAttach: PD.Boolean(false),
        showTooltip: PD.Boolean(true)
    })
});
