package edu.brynmawr.ple2;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.util.Random;

public class FragmentPhoto extends Fragment {


    private RelativeLayout mainView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        // set up the initial layout
        RelativeLayout ll = new RelativeLayout(getContext());
        ll.setId(View.generateViewId());
        ll.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ll.setBackgroundColor(Color.WHITE);
        mainView = ll;
        return ll;
    }

    /**
     * Generate a random color
     * @return the random color
     */
    private int randomColor() {
        Random r = new Random();
        return Color.rgb(r.nextInt(255), r.nextInt(255), r.nextInt(255));
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        layoutDoPhoto();
    }


    private void layoutDoPhoto() {
        mainView.removeAllViews();
        mainView.setBackgroundColor(Color.MAGENTA);
        ImageView iv = new ImageView(getContext());
        iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
        iv.setImageBitmap(MainActivity.thePicture);
        mainView.addView(iv, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void onStop() {
        super.onStop();
        Log.i("THISS", "onStop");
    }

    public void onDestroyView() {
        super.onDestroyView();
        Log.i("THISS", "onDestroyView AA");
        mainView.removeAllViews();
    }

    public void onDetach() {
        super.onDetach();
        Log.i("THISS", "onDetach");

    }

}
