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

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.umeng.commonsdk.debug.W;
import com.yutou.jianr_mg.Tools.Utils;

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

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

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

public class NightFragment extends Fragment implements WarLogInterface {
    private static NightFragment nightFragment;
    private JSONObject json;
    private JSONArray shipOV;
    private ListView list;

    private NightFragment() {

    }

    public static NightFragment getInstance() {
        if (nightFragment == null) {
            nightFragment = new NightFragment();
        }
        return nightFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        list = new ListView(getContext());
        list.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        list.setAdapter(new ListAdapter(new ArrayList<>()));
        return list;
    }

    private void initData() {

        try {
            List<ViewGroup> list=new ArrayList<>();
            list.add(nextItem("提督名字", json.getJSONObject("detailInfo").getString("username")));
            list.add(nextItem("提督等级", json.getJSONObject("detailInfo").getInt("level") + ""));
            list.add(nextItem("下一级需要经验", json.getJSONObject("detailInfo").getInt("exp") + ""));
            list.add(nextItem("拥有舰娘数", json.getJSONObject("detailInfo").getInt("shipNum") + ""));
            try {
                list.add(nextItem("打捞", json.getJSONArray("newShipVO").getJSONObject(0).getString("title")));
            } catch (Exception ignored) {

            }
            for (int i = 0; i < shipOV.length(); i++) {
                JSONObject selfShipResults = json.getJSONObject("warResult").getJSONArray("selfShipResults").getJSONObject(i);
                String mvp = selfShipResults.getInt("isMvp") == 1 ? "(MVP)" : "";
                try {
                    String levelUp="";
                    if(selfShipResults.getInt("isLevelUp")==1){
                        levelUp=" UP!!";
                    }
                    list.add(nextItem(mvp+shipOV.getJSONObject(i).getString("title")  ,
                            selfShipResults.getInt("exp") + "EXP (+" + selfShipResults.getInt("expAdd") + "exp)"+levelUp));
                }catch (Exception e){
                    list.add(nextItem(shipOV.getJSONObject(i).getString("title") + mvp,  "MAX(+" + selfShipResults.getInt("expAdd") + "exp)"));
                }

            }
            ((ListAdapter)this.list.getAdapter()).setData(list);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private LinearLayout nextItem(String title, String massage) {
        TextView titleView = new TextView(getContext());
        TextView messageView = new TextView(getContext());
        titleView.setText(title + ":");
        messageView.setText(massage);
        LinearLayout layout = new LinearLayout(getContext());
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.getPixelsFromDp(getActivity(),50));
        params.gravity= Gravity.CENTER;
        layout.setLayoutParams(params);
        layout.setGravity(Gravity.CENTER);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.addView(titleView);
        layout.addView(messageView);
        return layout;
    }

    @Override
    public void setData(JSONObject json) {
        try {
            this.json = new JSONObject(json.getJSONObject("WarReport").getString("message"));
            shipOV= new JSONObject(json.getJSONObject("DealNode").getString("message")).getJSONObject("warReport").getJSONArray("selfShips");
            initData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private class ListAdapter extends BaseAdapter {
        private List<ViewGroup> list;

        public ListAdapter(List<ViewGroup> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return list.get(position);
        }

        public void setData(List<ViewGroup> list) {
            this.list=list;
            notifyDataSetChanged();
        }
    }
}
