package ir.almahdi.sandogh_bsa_aza.Sepordeh_ozv;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
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.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

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.Global_var;
import ir.almahdi.sandogh_bsa_aza.R;

import static android.app.Activity.RESULT_OK;
/**
 * A simple {@link Fragment} subclass.
 */
public class frag_view_mosh_karbar extends Fragment
{@SuppressLint("StaticFieldLeak")
    private static ImageView img_user;
    private final String url = "http://sandogh-bs.makkh.ir/test_user.php";
    public static frag_view_mosh_karbar newInstance() {
        return new frag_view_mosh_karbar();
    }



    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             final Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_frag_view_mosh_karbar, container, false);
        FrameLayout frameLayout = view.findViewById(R.id.frm_moshkarbar);
                //شناساندن متغیرها
        TextView idname_fam = view.findViewById(R.id.txt_nam_famil);
        idname_fam.setClickable(true);
        TextView idozv = view.findViewById(R.id.txt_numozv);
        TextView idsalozv = view.findViewById(R.id.txt_salozv);
        TextView idmoj = view.findViewById(R.id.txt_mojodi);
        TextView idmab_hagh_oz_emsal = view.findViewById(R.id.mab_hagh_oz_emsal);
        img_user = view.findViewById(R.id.img_user);
        idozv.setText(Global_var.gid_ozviat);
        idname_fam.setText(Global_var.gname_ozv+" "+Global_var.glastname_ozv);
//load image to pic karbar
        String mImageUri;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(view.getContext());
        mImageUri = preferences.getString("image", null);
        if (mImageUri != null) {
            img_user.setImageURI(Uri.parse(mImageUri));
        } else {
            img_user.setImageResource(R.drawable.ic_person_add_black_24dp);
        }

        //
      idmab_hagh_oz_emsal.setText(Global_var.splitDigits(Integer.parseInt(Global_var.gmab_hagh_ozv.toString())));
      int gmojodi_ozv= Integer.parseInt(Global_var.gmojodi_ozv);
      idmoj.setText(Global_var.splitDigits(gmojodi_ozv) + " ریال");
      idmoj.setTextSize(20);
      idsalozv.setText(Global_var.gsal_mali);

        Glide
                .with(this)
                .load(R.drawable.ic_person_add_black_24dp)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .circleCrop()
                .into(img_user);
        //گرفتن عکس کاربر با کلیک روی تصویر
        img_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 openCamera();
            }

        });

       return view;

    }

    private void openCamera() {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);
    }

    @SuppressLint("ResourceType")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK) {
            Bitmap bp = (Bitmap) Objects.requireNonNull(data.getExtras()).get("data");
            // This is the key line item, URI specifies the name of the data
            String img_address = String.valueOf(bp);

                    // Saves image URI as string to Default Shared Preferences
                   SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(getView()).getContext());
                   SharedPreferences.Editor editor = preferences.edit();
                   editor.putString("pickarbar", String.valueOf(img_address));
                   editor.apply();
                   img_user.setImageBitmap(bp);
            Glide
                    .with(this)
                    .load(img_user.getDrawable().getCurrent())
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .circleCrop()
                    .into(img_user);

        }
    }

    }