package com.example.ricardo.dndform;

import android.app.Activity;
import android.provider.MediaStore;
import android.util.Log;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ricardo on 12/08/14.
 */
public class Character {

    private String playerName;

    private String characterName;

    private String race;

    private Integer level = 1;

    public static Character currentCharacter = new Character();

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public String getCharacterName() {
        return characterName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }

    @Override
    public String toString() {
        return "Character{" +
                "playerName='" + playerName + '\'' +
                ", characterName='" + characterName + '\'' +
                ", race='" + race + '\'' +
                ", level=" + level +
                ", skillLevels=" + skillLevels +
                '}';
    }

    public String getRace() {
        return race;
    }

    public void setRace(String race) {
        this.race = race;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public Map<Skill, Integer> getSkillLevels() {
        if (skillLevels.isEmpty()) {
            for (Skill skill : Skill.values()) {
                skillLevels.put(skill,0);
            }
        }
        return skillLevels;
    }

    public void setSkillLevels(Map<Skill, Integer> skillLevels) {
        this.skillLevels = skillLevels;
    }

    public enum Skill {
        STR,DEX,CON,INT,WIS,CHA;
    }

    private Map<Skill,Integer> skillLevels = new HashMap<Skill, Integer>();

    public static Integer getModifier(Integer skillLevel) {
        return skillLevel!=null && skillLevel>0 ? (skillLevel-10)/2 : 0;
    }

    public void save(Activity activity) {
        Gson gson = new Gson();
        String data = gson.toJson(this);
        try {
            FileWriter fileWriter = new FileWriter(activity.getFilesDir()+"/"+"dndform"+".data");
            fileWriter.write(data);
            fileWriter.flush();
            fileWriter.close();
            Log.w("dndform","Saved sucessfully");
        }
        catch (Exception e) {
            Log.e("dndform","Unable to save data");
            e.printStackTrace();
        }
    }

    public static Character load(Activity activity) {
        try {
            StringBuilder content = new StringBuilder();
            String line = null;
            File file = new File(activity.getFilesDir()+"/"+"dndform"+".data");
            if (file.exists()) {
                LineNumberReader fileReader = new LineNumberReader(new FileReader(activity.getFilesDir() + "/" + "dndform" + ".data"));
                while ((line = fileReader.readLine()) != null) {
                    content.append(line);
                }
                return currentCharacter = new Gson().fromJson(content.toString(),Character.class);
            }
            return currentCharacter = new Character();
        }
        catch (Exception e) {
            Log.e("dndform","Unable to load data");
            e.printStackTrace();
            return null;
        }

    }

}
