package ir.almahdi.sandogh_bsa_aza.pic_karbar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.Objects;

import ir.almahdi.sandogh_bsa_aza.R;

import static android.app.Activity.RESULT_OK;

/**
 * A simple {@link Fragment} subclass.
 */
public class pic_karbar_frg extends Fragment implements View.OnClickListener {
    private static final int PICK_IMAGE_REQUEST = 0;
    public pic_karbar_frg() {
        // Required empty public constructor
    }
    private String mImageUri;
    private ImageView img_user;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_pic_karbar_frg, container, false);
        img_user = view.findViewById(R.id.img_user);
        Glide
                .with(this)
                .load(R.drawable.ic_person_add_black_24dp)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .circleCrop()
                .into(img_user);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(view.getContext());
        mImageUri = preferences.getString("image", null);
       // preferences = getSharedPreferences("image", Context.MODE_PRIVATE);
        if (mImageUri != null) {
            img_user.setImageURI(Uri.parse(mImageUri));
        } else {
            img_user.setImageResource(R.drawable.ic_person_add_black_24dp);
        }

        //گرفتن عکس کاربر با کلیک روی تصویر
        img_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mImageUri != null) {
                    img_user.setImageURI(Uri.parse(mImageUri));
                } else {
                    img_user.setImageResource(R.drawable.ic_person_add_black_24dp);
                }
                  // get image
                  imageSelect();
            }

        });
        return  view ; }

 //Select an image

    private void imageSelect() {

        Intent intent;
        intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == PICK_IMAGE_REQUEST) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                // The user picked a image.
                // The Intent's data Uri identifies which item was selected.
                if (data != null) {

                    // This is the key line item, URI specifies the name of the data
                    mImageUri = String.valueOf(data.getData());

                    // Saves image URI as string to Default Shared Preferences
                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getView()).getContext());
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putString("image", String.valueOf(mImageUri));
                    editor.apply();
                    // Sets the ImageView with the Image URI
                    img_user.setImageURI(Uri.parse(mImageUri));
                    img_user.invalidate();
                }
            }
        }
    }

    @Override
    public void onClick(View v) {

    }
}
