package com.yutou.jianr_mg.Adapters;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
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.Model.HomeCard;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Log;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.views.HomeHeader;
import com.yutou.jianr_mg.views.ModActivity;
import com.yutou.jianr_mg.views.WebActivity;

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

;

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

public class HomeRecyclerAdapter extends RecyclerAdapter<HomeCard> {
    private ImageLoader imageLoader;
    private Activity activity;

    public HomeRecyclerAdapter(Context context) {
        super(context);
        this.activity = (Activity) context;
        imageLoader = Utils.initImageLoader(context);
    }

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

    private class CardRecordHolder extends BaseViewHolder<HomeCard> {
        private HomeHeader header;
        private ImageView imageView;
        private TextView textView;

        CardRecordHolder(ViewGroup parent, int layoutId) {
            super(parent, layoutId);
          header=HomeHeader.getHome(getContext());
          imageView=findViewById(R.id.card);
          textView=findViewById(R.id.text);
          imageLoader=Utils.initImageLoader(getContext());
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void setData(HomeCard data) {
            super.setData(data);
            Log.i("首页收据", JSON.toJSONString(data));
            header.flashHome();
            imageLoader.displayImage(data.getImageUrl(),imageView);
            textView.setText(" "+data.getText()+">> ");
        }

        @Override
        public void onItemViewClick(HomeCard data) {
            super.onItemViewClick(data);
            Intent intent = null;
            switch (data.getType()){
                case 0:
                    intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(data.getClick().toString()));

                break;
                case 1:
                    intent=new Intent(getContext(), ModActivity.class);
                    intent.putExtra("modId",data.getClick());
                    break;
                case 2:
                    intent=new Intent(getContext(), WebActivity.class);
                    intent.putExtra("url",data.getClick());
                    break;
            }
            if(intent!=null)
            getContext().startActivity(intent);
        }
    }
}
