package w.soulofpluto.posstock;

/*
 Create by Ramesh Babu
 */

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Plans> arrayList;
    public CustomAdapter(Context context, ArrayList<Plans> storesArrayList){
        this.context       = context;
        this.arrayList     = storesArrayList;
    }
    @NonNull
    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem = layoutInflater.inflate(R.layout.planslist_item, parent, false);
        CustomAdapter.ViewHolder viewHolder = new CustomAdapter.ViewHolder(listItem);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(@NonNull CustomAdapter.ViewHolder holder, final int position){
        final Plans items = arrayList.get(position);
        holder.plan_id.setText("PLAN ID : "+items.getPlanId());
        holder.plan_name.setText("NAME : "+items.getName());
        holder.view_section.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                final Dialog dialog = new Dialog(context);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.adapter_dialog);
                dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);
                dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                dialog.show();
                TextView section  = dialog.findViewById(R.id.plan_section);
                TextView division = dialog.findViewById(R.id.plan_division);
                ImageView close   = dialog.findViewById(R.id.close);
                section.setText(Html.fromHtml(Constants.Sec +items.getSection()));
                division.setText(Html.fromHtml(Constants.div+items.getDivision()));

                close.setOnClickListener(new OnClickListener(){
                   @Override
                   public void onClick(View view){
                     dialog.dismiss();
                    }
                });
            }
        });
        holder.start_scan.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
               PlanDetails.start(context,items);
            }
        });
     }
    @Override
    public int getItemCount(){
        return arrayList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView plan_id,plan_name;
        Button start_scan,view_section;
        public ViewHolder(View itemView){
            super(itemView);
            plan_id         = itemView.findViewById(R.id.plan_id);
            plan_name       = itemView.findViewById(R.id.plan_name);
            start_scan      = itemView.findViewById(R.id.start_scan);
            view_section    = itemView.findViewById(R.id.view_section);
        }
    }
}

