package org.intelehealth.app.activities.additionalDocumentsActivity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.firebase.crashlytics.FirebaseCrashlytics;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.intelehealth.app.R;
import org.intelehealth.app.app.IntelehealthApplication;
import org.intelehealth.app.database.dao.ImagesDAO;
import org.intelehealth.app.database.dao.ObsDAO;
import org.intelehealth.app.models.DocumentObject;

import org.intelehealth.app.models.dto.ObsDTO;
import org.intelehealth.app.utilities.StringUtils;
import org.intelehealth.app.utilities.UuidDictionary;
import org.intelehealth.app.utilities.exception.DAOException;

/**
 * Created by Dexter Barretto on 6/28/17.
 * Github : @dbarretto
 */

public class AdditionalDocumentAdapter extends RecyclerView.Adapter<AdditionalDocumentViewHolder> {

    int screen_height;
    int screen_width;

    private List<DocumentObject> documentList = new ArrayList<>();
    private Context context;
    private String filePath;
    String mEncounterUUID;
    ImagesDAO imagesDAO = new ImagesDAO();
    private static final String TAG = AdditionalDocumentAdapter.class.getSimpleName();

    public AdditionalDocumentAdapter(Context context, String edult,List<DocumentObject> documentList, String filePath) {
        this.documentList = documentList;
        this.context = context;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        screen_height = displayMetrics.heightPixels;
        screen_width = displayMetrics.widthPixels;
        mEncounterUUID=edult;
        this.filePath = filePath;

    }

    @Override
    public AdditionalDocumentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_additional_doc, null);
        AdditionalDocumentViewHolder rcv = new AdditionalDocumentViewHolder(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(final AdditionalDocumentViewHolder holder, final int position) {

//        holder.getDocumentNameTextView().setText(documentList.get(position).getDocumentName());
        holder.getDocumentNameTextView().setText
                (holder.itemView.getContext().getString(R.string.document_) + (position + 1));


        final File image = new File(documentList.get(position).getDocumentPhoto());

        Glide.with(context)
                .load(image)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .thumbnail(0.1f)
                .into(holder.getDocumentPhotoImageView());

        holder.getRootView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                displayImage(image);
            }
        });

        holder.getDeleteDocumentImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (image.exists()) image.delete();
                documentList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, documentList.size());
                String imageName = holder.getDocumentNameTextView().getText().toString();
                Log.d("ritika", "log" + imageName);
                try {
                    ObsDAO obsDAO = new ObsDAO();
                    obsDAO.getObsuuid(mEncounterUUID, UuidDictionary.COMPLEX_IMAGE_AD);
//                    Log.d("ritika", "getObsuuid===" + obsDAO.getObsuuid(mEncounterUUID, UuidDictionary.COMPLEX_IMAGE_AD));
//                    Log.d("ritika", "mEncounterUUID===" + mEncounterUUID);
//
                    try {
                        List<String> imageList = imagesDAO.isImageListObsExists(mEncounterUUID, UuidDictionary.COMPLEX_IMAGE_AD);
                        for (String images : imageList) {
                           Log.d("ritika","image"+images);

                        }
                        imagesDAO.deleteImageFromDatabase(imageList.get(position));
                    } catch (DAOException e) {
                        e.printStackTrace();
                    }
//                    imagesDAO.deleteImageFromDatabase(StringUtils.getFileNameWithoutExtensionString(imageName));
//                    imagesDAO.deleteImageFromDatabase(obsDAO.getObsuuid(mEncounterUUID, UuidDictionary.COMPLEX_IMAGE_AD));
                } catch (DAOException e) {
                    FirebaseCrashlytics.getInstance().recordException(e);
                }
            }
        });
    }

    public void add(DocumentObject doc) {
        boolean bool = documentList.add(doc);
        if (bool) Log.d(TAG, "add: Item added to list");
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return this.documentList.size();
    }


    public void displayImage(final File file) {
        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);


        final AlertDialog dialog = builder.create();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.image_confirmation_dialog, null);
        dialog.setView(dialogLayout);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface d) {
                ImageView imageView = dialog.findViewById(R.id.confirmationImageView);
                final ProgressBar progressBar = dialog.findViewById(R.id.progressBar);
                if (imageView != null) {
                    Glide.with(context)
                            .load(file)
                            .skipMemoryCache(true)
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .listener(new RequestListener<File, GlideDrawable>() {
                                @Override
                                public boolean onException(Exception e, File file, Target<GlideDrawable> target, boolean b) {
                                    if (progressBar != null) {
                                        progressBar.setVisibility(View.GONE);
                                    }
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(GlideDrawable glideDrawable, File file, Target<GlideDrawable> target, boolean b, boolean b1) {
                                    if (progressBar != null) {
                                        progressBar.setVisibility(View.GONE);
                                    }
                                    return false;
                                }
                            })
                            .override(screen_width, screen_height)
                            .into(imageView);
                }
            }
        });

        dialog.show();
        IntelehealthApplication.setAlertDialogCustomTheme(context, dialog);

    }
}

@@ -4161,424 +4161,9 @@
 ();%0A
-                Log.d(%22ritika%22, %22log%22 + imageName);%0A                try %7B%0A                    ObsDAO obsDAO = new ObsDAO();%0A                    obsDAO.getObsuuid(mEncounterUUID, UuidDictionary.COMPLEX_IMAGE_AD);%0A//                    Log.d(%22ritika%22, %22getObsuuid===%22 + obsDAO.getObsuuid(mEncounterUUID, UuidDictionary.COMPLEX_IMAGE_AD));%0A//                    Log.d(%22ritika%22, %22mEncounterUUID===%22 + mEncounterUUID);%0A//
+%0A
 %0A   
@@ -4410,23 +4410,20 @@
 g.d(
-%22ritika%22
+TAG
 ,%22image
+= 
 %22+im
@@ -4579,32 +4579,33 @@
 OException e) %7B%0A
+%0A
                 
@@ -4629,24 +4629,102 @@
 ackTrace();%0A
+                        FirebaseCrashlytics.getInstance().recordException(e);%0A
             
@@ -4852,267 +4852,8 @@
 ));%0A
-//                    imagesDAO.deleteImageFromDatabase(obsDAO.getObsuuid(mEncounterUUID, UuidDictionary.COMPLEX_IMAGE_AD));%0A                %7D catch (DAOException e) %7B%0A                    FirebaseCrashlytics.getInstance().recordException(e);%0A                %7D
 %0A   

Remove unused commented code