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

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.umeng.commonsdk.debug.W;
import com.yutou.jianr_mg.Fragments.GameDataFragments.WarLogFragments.Adapters.DaytimeListViewAdapter;

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class DaytimeFragment extends Fragment implements  WarLogInterface{
    private ListView listView;
    private DaytimeListViewAdapter adapter;
    private JSONObject warReport, json;
    private JSONArray shipVO;

    private static DaytimeFragment daytime;

    public static DaytimeFragment getInstance() {
        if (daytime == null) {
            daytime = new DaytimeFragment();
        }
        return daytime;
    }

    private DaytimeFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (listView != null) {
            return listView;
        }
        listView = new ListView(getContext());
        if (adapter == null) {
            adapter = new DaytimeListViewAdapter(getContext(), new ArrayList<>());
            listView.setAdapter(adapter);
        }
        initData();
        return listView;
    }

    public void setData(JSONObject json) {
        try {
            this.json = json;
            shipVO = new JSONObject(json.getJSONObject("DealNode").getString("message")).getJSONArray("shipVO");
            warReport = new JSONObject(json.getJSONObject("DealNode").getString("message")).getJSONObject("warReport");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initData() {
        if(adapter!=null)
            adapter.clear();
        List<WarData> list = new ArrayList<>();
        list.addAll(nextData("openAirAttack", WarData.OPEN_AIR));
        list.addAll(nextData("openMissileAttack", WarData.OPEN_MISSILE));
        list.addAll(nextData("openAntiSubAttack", WarData.OPEN_ANTISUB));
        list.addAll(nextData("openTorpedoAttack", WarData.OPEN_TORPEDO));
        list.addAll(nextData("normalAttacks", WarData.ATTACK_NORMAL));
        list.addAll(nextData("normalAttacks2", WarData.ATTACK_NORMAL_2));
        list.addAll(nextData("closeTorpedoAttack", WarData.CLOSE_TORPEDO));
        list.addAll(nextData("closeMissileAttack", WarData.CLOSE_MISSILE));
        try {
            if (warReport.getInt("canDoNightWar") == 1) {
                WarData data = new WarData();
                data.setAttackModel(WarData.NIGHT_WAR);
                list.add(data);
                list.addAll(nightWar());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (adapter != null)
            adapter.setData(list);
    }

    private List<WarData> nightWar() {
        List<WarData> list = new ArrayList<>();
        try {
            JSONArray nightAttacks = new JSONObject(json.getJSONObject("WarReport").getString("message")).getJSONObject("extraProgress").getJSONArray("nightAttacks");
            System.out.println("--------->夜战数量："+nightAttacks.length());
            for (int i = 0; i < nightAttacks.length(); i++) {
                System.out.println(nightAttacks.getJSONObject(i));
                WarData warData=WarData.getInstance(WarData.NIGHT_WAR,nightAttacks.getJSONObject(i),warReport);
                System.out.println(warData.toString());
                list.add(warData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private List<WarData> nextData(String name, int model) {
        List<WarData> list = new ArrayList<>();
        try {
            for (int i = 0; i < warReport.getJSONArray(name).length(); i++) {
                WarData warData = WarData.getInstance(model, warReport.getJSONArray(name).getJSONObject(i), warReport);
                if (warData != null)
                    list.add(warData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        daytime = null;
    }
}
