

import _ from "lodash";
import template from "./app-group-list-section.html";
import {initialiseData} from "../../../common";
import {CORE_API} from "../../../common/services/core-api-utils";
import toasts from "../../../svelte-stores/toast-store";

const bindings = {
    groupSubscriptions: "<"
};

const initialData = {
    showPublicSearch: false,
    availableGroups: [],
    recentlySubscribed: []
};


function removeUsedGroups(allGroups, existingSubscriptions) {
    const subscribedGroupIds = _.map(existingSubscriptions, "appGroup.id");
    return _.reject(allGroups, g => _.includes(subscribedGroupIds, g.id));
}


function controller(serviceBroker, $scope, $state) {

    const vm = initialiseData(this, initialData);

    function loadPublicGroups() {
        serviceBroker.loadViewData(CORE_API.AppGroupStore.findPublicGroups)
            .then(r => r.data)
            .then(publicGroups => removeUsedGroups(publicGroups, vm.groupSubscriptions))
            .then(availableGroups => vm.availableGroups = availableGroups);
    }


    function subscribeToGroup(group) {
        return serviceBroker
            .execute(CORE_API.AppGroupStore.subscribe, [group.id])
            .then(r => {
                vm.recentlySubscribed = _
                    .chain(vm.recentlySubscribed)
                    .concat(group)
                    .uniqBy(g => g.id)
                    .value();
                return r.data;
            })
    }


    function unsubscribeFromGroup(subscription) {
        return serviceBroker
            .execute(CORE_API.AppGroupStore.unsubscribe, [subscription.appGroup.id])
            .then(r => r.data);
    }

    vm.createNewGroup = () => {
        serviceBroker
            .execute(CORE_API.AppGroupStore.createNewGroup)
            .then(r => {
                toasts.success("New group created");
                $state.go("main.app-group.edit", { id: r.data })
            });
    };


    vm.unsubscribe = (subscription) => {
        unsubscribeFromGroup(subscription)
            .then(groupSubscriptions => vm.groupSubscriptions = groupSubscriptions)
            .then(() => toasts.warning(`Unsubscribed from: ${subscription.appGroup.name}`));
    };


    vm.deleteGroup = (group) => {
        if (! confirm("Really delete this group ? \n " + group.appGroup.name)) return;

        serviceBroker
            .execute(CORE_API.AppGroupStore.deleteGroup, [group.appGroup.id])
            .then(r => r.data)
            .then(groupSubscriptions => vm.groupSubscriptions = groupSubscriptions)
            .then(() => toasts.warning(`Deleted group: ${group.appGroup.name}`));

    };


    vm.onShowPublicSearch = () => {
        vm.showPublicSearch = true;
        loadPublicGroups();
    };


    vm.onHidePublicSearch = () => {
        vm.showPublicSearch = false;

    };

    $scope.$watch("$ctrl.selectedPublicGroup", selected => {
        if (selected && _.isObject(selected)) {
            subscribeToGroup(selected)
                .then(groupSubscriptions => vm.groupSubscriptions = groupSubscriptions)
                .then(() => toasts.success(`Subscribed to: ${selected.name}`))
                .then(() => vm.selectedPublicGroup = null);

        }
    });
}

controller.$inject = [
    "ServiceBroker",
    "$scope",
    "$state"
];


const component = {
    template,
    controller,
    bindings
};


export default {
    component,
    id: "waltzAppGroupListSection"
}