package com.yutou.jianr_mg.Adapters.GameDataAdapters;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Interfaces.ModInterface;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.ModUtils;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.Tools.ZipUtils;
import com.yutou.jianr_mg.views.WarLogActivity;

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

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.lemon.view.adapter.BaseViewHolder;
import cn.lemon.view.adapter.RecyclerAdapter;

public class WarAdapter extends RecyclerAdapter<List<Map<String, JSONObject>>> {
    ImageLoader loader;
    public WarAdapter(Context context) {
        super(context);
        loader= Utils.initImageLoader(context);
    }

    @Override
    public BaseViewHolder<List<Map<String, JSONObject>>> onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new MyHolder(parent, R.layout.item_team);
    }
    class MyHolder extends BaseViewHolder<List<Map<String, JSONObject>>>{
        private ImageView icon;
        private LinearLayout layout,hpLayout;
        private TextView name,time;
        private ProgressBar itemHp;
        public MyHolder(ViewGroup parent, int layoutId) {
            super(parent, layoutId);
            icon=findViewById(R.id.icon);
            layout=findViewById(R.id.items);
            hpLayout=findViewById(R.id.hpLayout);
            name=findViewById(R.id.title);
            itemHp=findViewById(R.id.itemHp);
            layout.setVisibility(View.GONE);
            itemHp.setVisibility(View.GONE);
            hpLayout.removeAllViews();
            time=new TextView(getContext());
            hpLayout.addView(time);
        }

        @Override
        public void setData(List<Map<String, JSONObject>> data) {
            super.setData(data);
            if(data.size()==0){
                return;
            }
            try {
                JSONObject warJson=new JSONObject(data.get(0).get("GetWarResult").getString("message"));
                int level=warJson.getJSONObject("warResult").getInt("resultLevel");
                String lev="";
                switch (level){
                    case 1:
                        lev="SS";
                        break;
                    case 2:
                        lev="S";
                        break;
                    case 3:
                        lev="A";
                        break;
                    case 4:
                        lev="B";
                        break;
                    case 5:
                        lev="C";
                        break;
                    case 6:
                        lev="D";
                        break;
                }
                JSONObject item=new JSONObject(data.get(0).get("DealNode").getString("message"));
                name.setText("("+data.get(0).get("data").get("mapId")+")"+item.getJSONObject("warReport").getString("enemyName")+"("+lev+")");
                time.setText(SimpleDateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.SHORT).format(new Date(Long.valueOf(data.get(0).get("DealNode").getString("time")))));
            } catch (Exception e) {
                name.setText("蹭了一下就溜了（无记录）");
            }
        }

        @Override
        public void onItemViewClick(List<Map<String, JSONObject>> data) {
            super.onItemViewClick(data);

            Map<String,JSONObject> map=data.get(0);
            JSONObject json=new JSONObject();
            try {
                json.put("data",map.get("data"));
                json.put("DealNode",map.get("DealNode"));
                json.put("WarReport",map.get("GetWarResult"));
                if(map.get("GetWarResult")==null){
                    Utils.toast(getContext(),"没有战斗数据");
                    return;
                }
                Intent intent=new Intent(getContext(), WarLogActivity.class);
                intent.putExtra("data", json.toString());
                getContext().startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();

            }



        }
    }
}
