package net.kiberion.psychobear.states.main.subviews;

import java.util.Collection;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableMap;

import lombok.Getter;
import net.kiberion.psychobear.model.PsychoBearActivity;
import net.kiberion.psychobear.model.global.PlayerModel;
import net.kiberion.psychobear.registries.ActivityRegistry;
import net.kiberion.psychobear.states.main.MainController;
import net.kiberion.psychobear.states.main.MainController.DayTime;
import net.kiberion.psychobear.states.main.MainView;
import net.kiberion.swampmachine.annotations.SubView;
import net.kiberion.swampmachine.api.elements.ButtonEntry;
import net.kiberion.swampmachine.api.invokables.LambdaInvokable;
import net.kiberion.swampmachine.api.sources.EntrySource;
import net.kiberion.swampmachine.entities.common.impl.CommonModelEntityDescriptor;
import net.kiberion.swampmachine.gui.annotations.Bound;
import net.kiberion.swampmachine.gui.annotations.BoundCompositions;
import net.kiberion.swampmachine.gui.elements.SwampTextButton;
import net.kiberion.swampmachine.gui.providers.ClickableElementSourceProvider;
import net.kiberion.swampmachine.gui.view.AbstractStateSubView;

@Component
@SubView(id = ScheduleSubView.SUB_VIEW_ID, parentViews = {MainView.class})
@BoundCompositions(compositions = { "mainSchedule" })
public class ScheduleSubView extends AbstractStateSubView<PlayerModel> {

    public static final String SUB_VIEW_ID = "scheduleSubView";

    private final Map<DayTime, CommonModelEntityDescriptor> dayTimeDescriptors = ImmutableMap
            .<DayTime, CommonModelEntityDescriptor> builder()
            .put(DayTime.DAY, new CommonModelEntityDescriptor("Day", DayTime.DAY.name()))
            .put(DayTime.EVENING, new CommonModelEntityDescriptor("Evening", DayTime.EVENING.name()))
            .put(DayTime.NIGHT, new CommonModelEntityDescriptor("Night", DayTime.NIGHT.name())).build();

    @Autowired
    @Getter
    private MainController controller;

    @Autowired
    private ActivityRegistry activityRegistry;

    @Bound(id = "nextTurn")
    private SwampTextButton nextTurn;

    private ClickableElementSourceProvider<CommonModelEntityDescriptor> activityGroupSourceProvider;

    public void updateActivityGroupList() {
        activityGroupSourceProvider.fillButtonList();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        activityGroupSourceProvider = new ClickableElementSourceProvider<CommonModelEntityDescriptor>() {

            @Override
            protected LambdaInvokable initOnClickEffect(CommonModelEntityDescriptor sourceElement) {
                return () -> {
                    controller.onDayTimeSelected((DayTime.valueOf(sourceElement.getId())));
                    return null;
                };
            }

            @Override
            public Collection<CommonModelEntityDescriptor> getElementList() {
                return dayTimeDescriptors.values();
            }

            @Override
            protected String initButtonText(CommonModelEntityDescriptor sourceElement) {
                PsychoBearActivity activity = controller.getActivity(DayTime.valueOf(sourceElement.getId()));
                StringBuilder text = new StringBuilder();
                text.append(sourceElement.getName());
                if (activity != null) {
                    CommonModelEntityDescriptor group = activityRegistry.getGroups().get(activity.getGroup());
                    Validate.notNull(group, "Unknown group: " + activity.getGroup());
                    text.append(": ").append(group.getName()).append(" - ").append(activity.getName());
                }
                return text.toString();
            }

        };
    }

    public EntrySource<ButtonEntry> getSchedule() {
        return activityGroupSourceProvider.getButtonSource();
    }

    @Override
    public void show() {
        super.show();
        updateActivityGroupList();

        boolean isAllActivitiesSelected = controller.isAllActivitiesSelected();
        //nextTurn.setDisabled(!isAllActivitiesSelected);
        //nextTurn.setVisible(isAllActivitiesSelected);
    }

}

@@ -1328,16 +1328,306 @@
 bView;%0A%0A
+import net.kiberion.swampmachine.assets.viewinfo.AnimationViewInfo;%0Aimport com.badlogic.gdx.graphics.g2d.SpriteBatch;%0Aimport com.badlogic.gdx.graphics.g2d.Animation;%0Aimport com.badlogic.gdx.graphics.g2d.TextureRegion;%0Aimport com.badlogic.gdx.graphics.Texture;%0Aimport com.badlogic.gdx.Gdx;%0A%0A
 @Compone
@@ -4596,10 +4596,1256 @@
 %0A    %7D%0A%0A
+    private boolean fire_once = true;%0A    private Animation animation;%0A    private float stateTime = 0.0f;%0A    private SpriteBatch spriteBatch;%0A%0A    @Override%0A    public void render() %7B%0A        if (fire_once) %7B%0A            fire_once = false;%0A%0A            animation = getAnimation(%22hacking/hacking.%22, 10, 0.04f);%0A            spriteBatch = new SpriteBatch();%0A        %7D%0A        stateTime += Gdx.graphics.getDeltaTime();%0A        spriteBatch.begin();%0A        spriteBatch.draw(animation.getKeyFrame(stateTime, true), 50, 50);%0A        spriteBatch.end();%0A    %7D%0A%0A    private final String PATH_PREFIX = %22ani/%22;%0A    private final String PATH_SUFFIX = %22.jpg%22;%0A%0A    public Animation getAnimation(String image, int frameCount, float frameDuration) %7B%0A        TextureRegion%5B%5D frames = new TextureRegion%5BframeCount%5D;%0A%0A        for (int i = 0; i %3C frameCount; ++i) %7B%0A            frames%5Bi%5D =%0A                new TextureRegion(%0A                    new Texture(%0A                        Gdx.files.internal(%0A                            PATH_PREFIX+image+String.format(%22%2504d%22, i)+PATH_SUFFIX%0A                        )%0A                    )%0A                );%0A        %7D%0A%0A        Animation animation = new Animation(frameDuration, frames);%0A        return animation;%0A    %7D%0A%0A
 %7D%0A

test animation