package de.frajul.endlessroll.levels;

import android.content.Context;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import de.frajul.endlessroll.R;
import de.frajul.endlessroll.main.DataStorageHandler;
import de.frajul.endlessroll.main.GameLog;
import de.frajul.endlessroll.sqlDatabase.MyDatabase;

public class LevelManager extends ArrayList<LevelPack> {

    public LevelManager(Context context, DataStorageHandler dataStorageHandler) throws Exception {
        String[] worldNames = context.getResources().getStringArray(R.array.world_names);
        String[] assets = context.getAssets().list("levelpacks");
        for (String asset : assets) {
            try {
                LevelPack pack = loadLevelPack(context, "levelpacks/" + asset);
                pack.setName(worldNames[pack.getId()]);

                MyDatabase database = dataStorageHandler.getDatabase();
                database.open();
                for (Level level : pack.getLevels())
                    database.readLevelProgress(level);
                database.readLevelPackLocked(pack);
                database.close();
                pack.tryToUnlockFirstLevel();
                if (pack.getId() == 1)
                    pack.setLocked(false);
                super.add(pack);
            } catch (Exception e) {
                GameLog.e(e);
            }
        }
        sortPacks();
    }

    private void sortPacks() {
        Collections.sort(this, packComparator);
    }

    private LevelPack loadLevelPack(Context context, String name) throws Exception {
        try {
            InputStream source = context.getAssets().open(name);
            Serializer serializer = new Persister();
            return serializer.read(LevelPack.class, source);
        } catch (Exception e) {
            throw new Exception("Could not load levelPack \"" + name + "\"", e);
        }
    }

    public int getTotalCollectedStarCount(){
        int count = 0;
        for(LevelPack levelPack : this)
            count += levelPack.getCollectedStarCount();
        return count;
    }

    public int getTotalCollectedEnergyCount(){
        int count = 0;
        for(LevelPack levelPack : this)
            count += levelPack.getCollectedEnergyCount();
        return count;
    }

    public LevelPack getNextLevelPack(LevelPack currentPack) {
        int searchedId = currentPack.getId() + 1;
        for (LevelPack pack : this) {
            if (pack.getId() == searchedId)
                return pack;
        }
        return null;
    }

    public LevelPack getPackWithId(int id) {
        for (LevelPack pack : this)
            if (pack.getId() == id)
                return pack;
        return null;
    }

    public void reset() {
        for (LevelPack pack : this) {
            pack.reset();
            if (pack.getId() == 1)
                pack.setLocked(false);
        }
    }

    private Comparator<LevelPack> packComparator = new Comparator<LevelPack>() {
        @Override
        public int compare(LevelPack lhs, LevelPack rhs) {
            return lhs.getId() - rhs.getId();
        }
    };

    //CHEAT
    public void unlockAllPacks() {
        for (LevelPack levelPack : this)
            levelPack.setLocked(false);
    }

    public void unlockAllLevels() {
        for (LevelPack levelPack : this) {
            for (Level level : levelPack.getLevels())
                level.setLocked(false);
        }
    }

}
