package com.yutou.jianr_mg.Adapters;

import android.annotation.SuppressLint;
import android.content.Context;
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.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Data.JianNiang;
import com.yutou.jianr_mg.Data.ShipEquipmnt;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.JianRUtils;
import com.yutou.jianr_mg.Tools.Utils;

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

public class MapUserTeamRecyclierAdapter extends RecyclerAdapter<JSONObject> {
    ImageLoader loader;

    public MapUserTeamRecyclierAdapter(Context context) {
        super(context);
        loader = Utils.initImageLoader(context);
    }

    @Override
    public BaseViewHolder<JSONObject> onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent, R.layout.item_team);
    }

    private class ViewHolder extends BaseViewHolder<JSONObject> {
        TextView title, hp, hpMax;
        ImageView icon, icon_, item1, item2, item3, item4;
        ProgressBar hpBar;
        LinearLayout hpLayout;

        public ViewHolder(ViewGroup parent, int layoutId) {
            super(parent, layoutId);
            hp = findViewById(R.id.hp);
            hpMax = findViewById(R.id.hpMax);
            hpBar = findViewById(R.id.itemHp);
            title = findViewById(R.id.title);
            icon = findViewById(R.id.icon);
            icon_ = findViewById(R.id.icon_);
            item1 = findViewById(R.id.item1);
            item2 = findViewById(R.id.item2);
            item3 = findViewById(R.id.item3);
            item4 = findViewById(R.id.item4);
            hpLayout = findViewById(R.id.hpLayout);
            hpBar.setVisibility(View.GONE);
            hpLayout.setVisibility(View.GONE);

        }

        @SuppressLint("SetTextI18n")
        @Override
        public void setData(JSONObject data) {
            super.setData(data);
            JianNiang jianNiang = JianRUtils.getJianNiangByCId(data.getInteger("cid"));
            if (jianNiang == null)
                return;
            title.setText(jianNiang.getTitle() + "  Level:" + data.getInteger("level"));
            loader.displayImage(JianRUtils.getShipImageByCid(data.getInteger("cid"), true), icon);
            JSONArray equipment = data.getJSONArray("equipment");
            if (equipment.getString(0) != null) {
                loader.displayImage(JianRUtils.getShipImageByCid(Integer.parseInt(equipment.getString(0)), false), item1);
                item1.setOnClickListener(view -> {
                    ShipEquipmnt shipEquipmnt = JianRUtils.getEquipmnt(Integer.parseInt(equipment.getString(0)), -2);
                    if (shipEquipmnt != null)
                        Utils.toast(getContext(), shipEquipmnt.getTitle());
                });
            }
            if (equipment.getString(1) != null) {
                loader.displayImage(JianRUtils.getShipImageByCid(Integer.parseInt(equipment.getString(1)), false), item2);
                item2.setOnClickListener(view -> {
                    ShipEquipmnt shipEquipmnt = JianRUtils.getEquipmnt(Integer.parseInt(equipment.getString(1)), -2);
                    if (shipEquipmnt != null)
                        Utils.toast(getContext(), shipEquipmnt.getTitle());
                });
            }
            if (equipment.getString(2) != null) {
                loader.displayImage(JianRUtils.getShipImageByCid(Integer.parseInt(equipment.getString(2)), false), item3);
                item3.setOnClickListener(view -> {
                    ShipEquipmnt shipEquipmnt = JianRUtils.getEquipmnt(Integer.parseInt(equipment.getString(2)), -2);
                    if (shipEquipmnt != null)
                        Utils.toast(getContext(), shipEquipmnt.getTitle());
                });
            }
            if (equipment.getString(3) != null) {
                loader.displayImage(JianRUtils.getShipImageByCid(Integer.parseInt(equipment.getString(3)), false), item4);
                item4.setOnClickListener(view -> {
                    ShipEquipmnt shipEquipmnt = JianRUtils.getEquipmnt(Integer.parseInt(equipment.getString(3)), -2);
                    if (shipEquipmnt != null)
                        Utils.toast(getContext(), shipEquipmnt.getTitle());
                });
            }

        }
    }

}
