package com.example.test;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public  class AdapterMask extends BaseAdapter {

    private Context mContext;
    private List<DataModal> mListMask;

    public AdapterMask(Context mContext, List<DataModal> listProduct)
    {
        this.mContext = mContext;
        this.mListMask = listProduct;
    }
//List<DataModal> maskList;
    @Override
    public int getCount()
    {
        return mListMask.size();
    }
    @Override
    public  Object getItem(int position)
    {
        return mListMask.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mListMask.get(position).getKod_teacher();
    }

    private Bitmap getUserImage(String encodeImg)
    {
        if(encodeImg != null && !encodeImg.equals("null"))
        {
            byte[] bytes = Base64.decode(encodeImg, Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(bytes, 0,bytes.length);
        }
        else
        {
            return BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher_foreground);
        }
    }

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        View v = View.inflate(mContext, R.layout.mask, null);
        TextView Fam = v.findViewById(R.id.Fam);
        TextView Patronymic = v.findViewById(R.id.Patronymic);
        TextView Subject = v.findViewById(R.id.Subject);
        TextView Name = v.findViewById(R.id.Name);
        ImageView Images = v.findViewById(R.id.image);

        DataModal dataModal = mListMask.get(position);
        Fam.setText(dataModal.getSurname());
        Patronymic.setText(dataModal.getPatronymic());
        Name.setText(dataModal.getName());
        Subject.setText(dataModal.getSubject());

        Images.setImageBitmap(getUserImage(dataModal.getImages()));

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, Edit.class);
                intent.putExtra(DataModal.class.getSimpleName(),dataModal);
                mContext.startActivity(intent);
            }
        });
        return  v;
    }




}
