package com.yutou.jianr_mg.Adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Data.MAppMg;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.views.BaseActivity;
import com.yutou.jianr_mg.views.ModListActivity;


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


/**
 * Created by 58381 on 2018/1/20.
 */

public class ModListRecyclerAdapter extends RecyclerAdapter<MAppMg> {
    private ImageLoader imageLoader;

    public ModListRecyclerAdapter(Context context) {
        super(context);
        imageLoader = Utils.initImageLoader(getContext());
    }

    @Override
    public BaseViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new CardRecordHolder(parent, R.layout.item_mg_home);
    }

    private class CardRecordHolder extends BaseViewHolder<MAppMg> {
        private ImageView icon;
        private TextView name;

        CardRecordHolder(ViewGroup parent, int layoutId) {
            super(parent, layoutId);
            icon = findViewById(R.id.image);
            name = findViewById(R.id.title);
        }

        @Override
        public void setData(MAppMg data) {
            super.setData(data);
            if (data.getIcon() != null && !"null".equals(data.getIcon())) {
                String url=data.getIcon();
                if(!url.contains("http://")&&!url.contains("https://")){
                    try {
                        url=AppData.appConfig.getDownloadhome() + data.getIcon();
                    }catch (Exception e){
                        e.printStackTrace();
                        url="#";
                    }

                }
                imageLoader.displayImage(url, icon);
            }else{
                icon.setImageResource(R.mipmap.icon_logo);
                icon.getLayoutParams().width=Utils.getPixelsFromDp((Activity) getContext(),84);
                icon.getLayoutParams().height=Utils.getPixelsFromDp((Activity) getContext(),84);
            }
            name.setText(data.getName());
        }

        @Override
        public void onItemViewClick(MAppMg data) {
            super.onItemViewClick(data);
            if (data.getUrl() != null) {
                Intent intent = new Intent(getContext(), BaseActivity.class);
                intent.putExtra("jar", data.getUrl().split("/")[data.getUrl().split("/").length - 1]);
                intent.putExtra("activityName", data.getMainFunction());
                getContext().startActivity(intent);
                return;
            }
            Intent intent = new Intent(getContext(), ModListActivity.class);
            intent.putExtra("mg", JSON.toJSONString(data));
            getContext().startActivity(intent);

        }
    }
}
