package com.yutou.jianr_mg.Adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.yutou.jianr_mg.Data.TeamData;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Utils;

import java.util.List;

public class MagicTeamDataAdapter extends BaseAdapter {
    List<TeamData> list;
    Context context;
    ImageLoader loader;
    boolean spoiler = true;


    public MagicTeamDataAdapter(List<TeamData> list, Context context) {
        this.list = list;
        this.context = context;
        loader = Utils.initImageLoader(context);
    }

    public void setSpoiler(boolean spoiler) {
      //  this.spoiler = spoiler;
    }

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

    @Override
    public TeamData getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    ItemViews item = null;
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view == null) {
            item = new ItemViews();
            view = LayoutInflater.from(context).inflate(R.layout.item_team, viewGroup, false);
            item.hp = view.findViewById(R.id.hp);
            item.hpMax = view.findViewById(R.id.hpMax);
            item.hpBar = view.findViewById(R.id.itemHp);
            item.title = view.findViewById(R.id.title);
            item.icon = view.findViewById(R.id.icon);
            item.icon_ = view.findViewById(R.id.icon_);
            item.item1 = view.findViewById(R.id.item1);
            item.item2 = view.findViewById(R.id.item2);
            item.item3 = view.findViewById(R.id.item3);
            item.item4 = view.findViewById(R.id.item4);
            item.hpLayout = view.findViewById(R.id.hpLayout);
            view.setTag(item);
        } else {
            item = (ItemViews) view.getTag();
        }
        TeamData data = list.get(i);
        item.hp.setText(data.getHp());
        if (!spoiler) {
            item.hp.setText("???");
        }
        item.hpMax.setText(data.getHpMax());
        item.hpBar.setMax(100);

        if (Float.valueOf(data.getHp()) > (Float.valueOf(data.getHpMax()) / 2)) {
            ClipDrawable d = new ClipDrawable(new ColorDrawable(Color.DKGRAY), Gravity.START, ClipDrawable.HORIZONTAL);
            item.hpBar.setProgressDrawable(d);
            System.out.println(data.getTitle()+"设置颜色：绿色"+data.getHp()+"/"+data.getHpMax());
        } else if (Float.valueOf(data.getHp()) < (Float.valueOf(data.getHpMax()) / 2)&&Float.valueOf(data.getHp()) >= (Float.valueOf(data.getHpMax()) / 2/ 2)) {
            ClipDrawable d = new ClipDrawable(new ColorDrawable(Color.YELLOW), Gravity.START, ClipDrawable.HORIZONTAL);
            item.hpBar.setProgressDrawable(d);
            System.out.println(data.getTitle()+"设置颜色：黄色"+data.getHp()+"/"+data.getHpMax());
        } else if (Float.valueOf(data.getHp()) < (Float.valueOf(data.getHpMax()) / 2/ 2) ) {
            ClipDrawable  d = new ClipDrawable(new ColorDrawable(Color.RED), Gravity.START, ClipDrawable.HORIZONTAL);
            item.hpBar.setProgressDrawable(d);
            System.out.println(data.getTitle()+"设置颜色：红色"+data.getHp()+"/"+data.getHpMax());
        }else{
            System.out.println("没有设置颜色");
        }
        if(data.getTitle().equals("凰")){
            item.hpBar.setMax(100);
            item.hpBar.setProgress(86);
            System.out.println("赤城的参数"+getHp(data.getHp(),data.getHpMax())+" "+(Float.valueOf(data.getHp()) > (Float.valueOf(data.getHpMax()) / 2)));

        }else
        item.hpBar.setProgress((int) getHp(data.getHp(),data.getHpMax()));
        if (!spoiler) {
           // item.hpBar.setProgress(0);
        }
        item.title.setText(data.getTitle());
        item.title.setVisibility(View.VISIBLE);
        item.hp.setVisibility(View.VISIBLE);
        item.hpMax.setVisibility(View.VISIBLE);
        item.title.setTextColor(Color.BLACK);
        item.hp.setTextColor(Color.BLACK);
        item.hpMax.setTextColor(Color.BLACK);
        if (!data.isUser()) {
                    ((LinearLayout.LayoutParams) item.hpLayout.getLayoutParams()).gravity = Gravity.END;
            item.title.setGravity(Gravity.END);
            item.icon.setVisibility(View.GONE);
            item.icon_.setVisibility(View.VISIBLE);
        } else {
            ((LinearLayout.LayoutParams) item.hpLayout.getLayoutParams()).gravity = Gravity.LEFT;
            item.title.setGravity(Gravity.LEFT);
            item.icon.setVisibility(View.VISIBLE);
            item.icon_.setVisibility(View.GONE);
        }
        if (data.getImageUrl() != null) {
            if (data.isUser()) {
                loader.displayImage(data.getImageUrl(), item.icon);
            } else {
                loader.displayImage(data.getImageUrl(), item.icon_);
            }
        }
        if (data.getItem1Url() != null) {
            loader.displayImage(data.getItem1Url(), item.item1, new imageListener());
            item.item1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            item.item1.setVisibility(View.VISIBLE);
        } else {
            item.item1.setVisibility(View.GONE);
        }
        if (data.getItem2Url() != null) {
            loader.displayImage(data.getItem2Url(), item.item2, new imageListener());
            item.item2.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            item.item2.setVisibility(View.VISIBLE);
        } else {
            item.item2.setVisibility(View.GONE);
        }
        if (data.getItem3Url() != null) {
            loader.displayImage(data.getItem3Url(), item.item3, new imageListener());
            item.item3.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            item.item3.setVisibility(View.VISIBLE);
        } else {
            item.item3.setVisibility(View.GONE);
        }
        if (data.getItem4Url() != null) {
            loader.displayImage(data.getItem4Url(), item.item4, new imageListener());
            item.item4.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            item.item4.setVisibility(View.VISIBLE);
        } else {
            item.item4.setVisibility(View.GONE);
        }
        item.reload();
        return view;
    }

    public void setData(List<TeamData> enemyTeam) {
        this.list.clear();
        this.list.addAll(enemyTeam);
    }

    public List<TeamData> getData() {
        return this.list;
    }

    public void reload() {
        if(item!=null){
            item.reload();
        }
    }
    public float getHp(String hp,String maxHp){
        float h=Float.parseFloat(hp);
        float max=Float.parseFloat(maxHp);
        return 100/max*h;
    }
    class ItemViews {
        TextView title, hp, hpMax;
        ImageView icon, icon_, item1, item2, item3, item4;
        ProgressBar hpBar;
        LinearLayout hpLayout;

        void reload() {
            title.postInvalidate();
            hp.postInvalidate();
            hpMax.postInvalidate();
            hpBar.postInvalidate();
        }
    }

    class imageListener implements ImageLoadingListener {
        @Override
        public void onLoadingStarted(String imageUri, View view) {

        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            ImageView imageView = (ImageView) view;
            imageView.setImageResource(R.mipmap.icon_logo);
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {

        }
    }
}
