package main;

import java.awt.Graphics;
import utils.DynamicImage;
import utils.Utils;
import java.awt.Color;
import gamegui.Animation;

public class Model
{
    Direction direction;
    Action action;
    private Animation[] anims;
    
    public Model(final String name, final Color bgColor, final Color shadowColor, final int[] numFramesArr) {
        this.anims = new Animation[48];
        this.direction = Direction.North;
        this.action = Action.Walking;
        int numFrames = numFramesArr[0];
        Direction[] values;
        for (int length = (values = Direction.values()).length, i = 0; i < length; ++i) {
            final Direction dir = values[i];
            final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
            for (int x = 0; x < numFrames; ++x) {
                curAnim.addFrame(new DynamicImage("creatures/" + name + "/walking/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
            }
            curAnim.setWidth(96);
            curAnim.setHeight(96);
            this.addAnimation(dir, Action.Walking, curAnim);
        }
        numFrames = numFramesArr[1];
        Direction[] values2;
        for (int length2 = (values2 = Direction.values()).length, j = 0; j < length2; ++j) {
            final Direction dir = values2[j];
            final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
            for (int x = 0; x < numFrames; ++x) {
                curAnim.addFrame(new DynamicImage("creatures/" + name + "/standing/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
            }
            curAnim.setWidth(96);
            curAnim.setHeight(96);
            this.addAnimation(dir, Action.Standing, curAnim);
        }
        numFrames = numFramesArr[2];
        Direction[] values3;
        for (int length3 = (values3 = Direction.values()).length, k = 0; k < length3; ++k) {
            final Direction dir = values3[k];
            final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
            for (int x = 0; x < numFrames; ++x) {
                curAnim.addFrame(new DynamicImage("creatures/" + name + "/attacking/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
            }
            curAnim.setWidth(96);
            curAnim.setHeight(96);
            this.addAnimation(dir, Action.Attacking, curAnim);
        }
        numFrames = numFramesArr[3];
        Direction[] values4;
        for (int length4 = (values4 = Direction.values()).length, l = 0; l < length4; ++l) {
            final Direction dir = values4[l];
            final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, true);
            for (int x = 0; x < numFrames; ++x) {
                curAnim.addFrame(new DynamicImage("creatures/" + name + "/beenhit/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
            }
            curAnim.setWidth(96);
            curAnim.setHeight(96);
            this.addAnimation(dir, Action.BeenHit, curAnim);
        }
        numFrames = numFramesArr[4];
        Direction[] values5;
        for (int length5 = (values5 = Direction.values()).length, n = 0; n < length5; ++n) {
            final Direction dir = values5[n];
            final Animation curAnim = new Animation(dir.toString(), 0, 0, 40, 80, 100, false);
            for (int x = 0; x < numFrames; ++x) {
                curAnim.addFrame(new DynamicImage("creatures/" + name + "/dying/" + Utils.shortString(dir) + String.format("%04d", x) + ".png", bgColor, shadowColor));
            }
            curAnim.setWidth(128);
            curAnim.setHeight(128);
            this.addAnimation(dir, Action.Dying, curAnim);
        }
    }
    
    public Model(final Model copy) {
        this.direction = copy.direction;
        this.action = copy.action;
        this.anims = new Animation[48];
        for (int x = 0; x < this.anims.length; ++x) {
            if (copy.anims[x] != null) {
                this.anims[x] = new Animation(copy.anims[x]);
            }
        }
    }
    
    public void addAnimation(final Direction dir, final Action action, final Animation anim) {
        this.anims[this.getIndex(dir, action)] = anim;
    }
    
    public void draw(final Graphics g, final int x, final int y) {
        if (this.anims[this.getIndex(this.direction, this.action)] != null) {
            this.anims[this.getIndex(this.direction, this.action)].draw(g, x, y);
        }
    }
    
    public int getHeight() {
        return this.getAnimation(this.direction, this.action).getHeight();
    }
    
    public int getWidth() {
        return this.getAnimation(this.direction, this.action).getWidth();
    }
    
    private int getIndex(final Direction dir, final Action action) {
        int pos = 0;
        switch (action) {
            case Running: {
                pos = 8;
                break;
            }
            case Standing: {
                pos = 16;
                break;
            }
            case Attacking: {
                pos = 24;
                break;
            }
            case BeenHit: {
                pos = 32;
                break;
            }
            case Dying: {
                pos = 40;
                break;
            }
        }
        switch (dir) {
            case NorthEast: {
                ++pos;
                break;
            }
            case East: {
                pos += 2;
                break;
            }
            case SouthEast: {
                pos += 3;
                break;
            }
            case South: {
                pos += 4;
                break;
            }
            case SouthWest: {
                pos += 5;
                break;
            }
            case West: {
                pos += 6;
                break;
            }
            case NorthWest: {
                pos += 7;
                break;
            }
        }
        return pos;
    }
    
    public Animation getAnimation(final Direction dir, final Action action) {
        return this.anims[this.getIndex(dir, action)];
    }
    
    public Animation getCurrentAnimation() {
        return this.getAnimation(this.direction, this.action);
    }
}
