package com.yutou.jianr_mg.Fragments.GameDataFragments.WarLogFragments;

import com.umeng.commonsdk.debug.W;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Arrays;

public class WarData {
    public final static int OPEN_AIR = 0;
    public final static int OPEN_MISSILE = 1;
    public final static int OPEN_ANTISUB = 2;
    public final static int OPEN_TORPEDO = 3;
    public final static int ATTACK_NORMAL = 4;
    public final static int ATTACK_NORMAL_2 = 5;
    public final static int CLOSE_TORPEDO = 6;
    public final static int CLOSE_MISSILE = 7;
    public final static int NIGHT_WAR = 8;
    private String title;
    private String userName;
    private long cid;
    private int fromIndex;
    private String[] enemy;
    private int[] damage;
    private boolean userAttack;
    private int attackModel;

    public String getAttackModel() {
        String model;
        switch (attackModel) {
            case WarData.OPEN_AIR:
                model = "航空战";
                break;
            case WarData.ATTACK_NORMAL:
                model = "炮击";
                break;
            case WarData.ATTACK_NORMAL_2:
                model = "次轮炮击";
                break;
            case WarData.OPEN_MISSILE:
                model = "导弹战";
                break;
            case WarData.OPEN_TORPEDO:
                model = "开幕雷击";
                break;
            case WarData.OPEN_ANTISUB:
                model = "开幕反潜";
                break;
            case WarData.CLOSE_TORPEDO:
                model = "闭幕雷击";
                break;
            case WarData.CLOSE_MISSILE:
                model = "闭幕导弹";
                break;
            case WarData.NIGHT_WAR:
                model = "夜战";
                break;
            default:
                model = "未知攻击";
        }
        return model;
    }

    public void setAttackModel(int attackModel) {
        this.attackModel = attackModel;
    }

    public boolean isUserAttack() {
        return userAttack;
    }

    public void setUserAttack(boolean userAttack) {
        this.userAttack = userAttack;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public long getCid() {
        return cid;
    }

    public void setCid(long cid) {
        this.cid = cid;
    }

    public int getFromIndex() {
        return fromIndex;
    }

    public void setFromIndex(int fromIndex) {
        this.fromIndex = fromIndex;
    }

    public String[] getEnemy() {
        return enemy;
    }

    public void setEnemy(String[] enemy) {
        this.enemy = enemy;
    }

    public int[] getDamage() {
        return damage;
    }

    public void setDamage(int[] damage) {
        this.damage = damage;
    }

    public static WarData getInstance(int attackModel, JSONObject json, JSONObject warReport) {
        WarData warData = null;
        try {
            warData = new WarData();
            warData.userAttack = json.getInt("attackSide") == 1;
            JSONArray array = warData.isUserAttack() ? warReport.getJSONArray("selfShips") : warReport.getJSONArray("enemyShips");
            warData.setUserName(array.getJSONObject(json.getInt("fromIndex")).getString("title"));
            warData.setCid(array.getJSONObject(json.getInt("fromIndex")).getLong("shipCid"));
            int[] damages = new int[json.getJSONArray("damages").length()];
            for (int i = 0; i < json.getJSONArray("damage").length(); i++) {
                damages[i] = json.getJSONArray("damage").getInt(i);
            }
            warData.setDamage(damages);
            String[] enemyNames = new String[json.getJSONArray("targetIndex").length()];
            array = !warData.isUserAttack() ? warReport.getJSONArray("selfShips") : warReport.getJSONArray("enemyShips");
            for (int i = 0; i < json.getJSONArray("targetIndex").length(); i++) {
                enemyNames[i] = array.getJSONObject(json.getJSONArray("targetIndex").getInt(i)).getString("title");
            }
            warData.setEnemy(enemyNames);
            warData.setAttackModel(attackModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return warData;
    }

    @Override
    public String toString() {
        return "WarData{" +
                "title='" + title + '\'' +
                ", userName='" + userName + '\'' +
                ", cid=" + cid +
                ", fromIndex=" + fromIndex +
                ", enemy=" + Arrays.toString(enemy) +
                ", damage=" + Arrays.toString(damage) +
                ", userAttack=" + userAttack +
                ", attackModel=" + attackModel +
                '}';
    }
}
