package main;

import java.io.BufferedReader;
import utils.Utils;
import java.util.HashMap;
import java.util.ArrayList;
import java.awt.FontMetrics;
import java.awt.Point;

public class NPC extends Creature
{
    public Dialog dialog;
    int state;
    
    public NPC(final String name, final int xOffset, final int yOffset) {
        super(name, xOffset, yOffset);
        final boolean b = true;
        this.hitpoints = (b ? 1 : 0);
        this.maxHitpoints = (b ? 1 : 0);
        this.dialog = null;
        this.state = 0;
    }
    
    public NPC(final NPC cr) {
        super(cr);
        this.dialog = cr.dialog;
    }
    
    @Override
    public NPC copy(final Point newLoc) {
        final NPC newCr = new NPC(this);
        newCr.initLoc(newLoc);
        return newCr;
    }
    
    public void loadDialog(final String fileName, final FontMetrics metrics, final int width) {
        final ArrayList<Dialog> dialogs = new ArrayList<Dialog>();
        final HashMap<Integer, Dialog> dialogReferences = new HashMap<Integer, Dialog>();
        final ArrayList<ArrayList<Integer>> referencesList = new ArrayList<ArrayList<Integer>>();
        try {
            final BufferedReader in = Utils.loadTextFile("dialog/" + fileName);
            String curLine = in.readLine();
            while (in.ready()) {
                final int num = Integer.parseInt(curLine.substring(1));
                final Dialog curDialog = new Dialog(in.readLine(), metrics, width);
                final ArrayList<Integer> curReferences = new ArrayList<Integer>();
                dialogReferences.put(num, curDialog);
                in.readLine();
                curLine = in.readLine();
                do {
                    if (curLine.equals("[NULL]")) {
                        curReferences.add(null);
                    }
                    else {
                        final int ref = Integer.parseInt(curLine.substring(1, curLine.indexOf("]")));
                        curReferences.add(ref);
                    }
                    curLine = in.readLine();
                    if (curLine.substring(0, 1).equals("%")) {
                        curDialog.addTrigger(curDialog.getNumOptions(), Integer.parseInt(curLine.substring(1)));
                        curDialog.addOption(in.readLine(), null);
                    }
                    else {
                        curDialog.addOption(curLine, null);
                    }
                    if (!in.ready()) {
                        break;
                    }
                    in.readLine();
                    curLine = in.readLine();
                } while (!curLine.substring(0, 1).equals("#"));
                dialogs.add(curDialog);
                referencesList.add(curReferences);
            }
            for (int numDialogs = dialogs.size(), x = 0; x < numDialogs; ++x) {
                for (int numOptions = dialogs.get(x).getNumOptions(), y = 0; y < numOptions; ++y) {
                    if (referencesList.get(x).get(y) != null) {
                        dialogs.get(x).changeReference(y, dialogReferences.get(referencesList.get(x).get(y)));
                    }
                }
            }
            this.dialog = dialogs.get(0);
            in.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public Dialog processDialog(final Dialog curDialog, final int selection, final Player player) {
        if (curDialog.getTrigger(selection) != null) {
            if (this.name.equals("Seer")) {
                this.handleTriggers1(curDialog.getTrigger(selection), player);
            }
            if (this.name.equals("Wayfarer")) {
                this.handleTriggers2(curDialog.getTrigger(selection), player);
            }
        }
        return curDialog.getReference(selection);
    }
    
    private void handleTriggers1(final int triggerNum, final Player player) {
        if (this.state == 0) {
            switch (triggerNum) {
                case 1: {
                    this.loadDialog("healDialog.txt", this.dialog.metrics, this.dialog.width);
                    this.state = 1;
                    break;
                }
            }
        }
        else if (this.state == 1) {
            switch (triggerNum) {
                case 1: {
                    player.hitpoints = player.maxHitpoints;
                    break;
                }
            }
        }
    }
    
    private void handleTriggers2(final int triggerNum, final Player player) {
        if (this.state == 0) {
            switch (triggerNum) {
                case 1: {
                    player.pickUp(LostHavenRPG.itemMap.get("Long Sword"));
                    player.pickUp(LostHavenRPG.itemMap.get("Cloth Boots"));
                    this.state = 1;
                    break;
                }
            }
        }
    }
}
