package com.example.mynotepad.MenuFeatures.Pics;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.AddTextToPicDialog;
import com.example.mynotepad.Dialogs.AddTxtToPicsDialog.OnSavePicListenerClick;
import com.example.mynotepad.MenuFeatures.Pics.Camera.CameraActivity;
import com.example.mynotepad.MenuFeatures.Utils;
import com.example.mynotepad.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class ChoosingPicsFragment extends Fragment {

    private LinearLayout camera, gallery;
    private String newFilePath;
    private ConstraintLayout constraintLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ((PicsActivity) getActivity()).getSupportActionBar().setTitle("Choose Pic");
        return inflater.inflate(R.layout.fragment_choosing_pics, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        setBg(Utils.sTheme);
        addListeners();
    }

    private void setBg(String sTheme) {
        switch (sTheme) {
            case "dark":
                constraintLayout.setBackgroundResource(R.drawable.main_bg_dark);
                break;
            case "bright":
                constraintLayout.setBackgroundResource(R.drawable.main_bg_bright);
                break;
        }
    }

    private void init(View view) {
        camera = view.findViewById(R.id.cameraBtn);
        gallery = view.findViewById(R.id.galleryBtn);
        constraintLayout = view.findViewById(R.id.constrainLayout);
    }

    private void addListeners() {
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(getActivity(), CameraActivity.class), 2);
            }
        });

        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        newFilePath = "";
        System.out.println("================================================================================================");
        if (data != null) {
            if (requestCode == 1) {// get image from gallery
                newFilePath = data.getData().getPath();
                InputStream stream = null;
                try {
                    stream = getActivity().getContentResolver().openInputStream(data.getData());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                System.out.println(")))))))))))))))))))))))))))))))))))))))))))))))" + newFilePath);
                showDialogOnResult(BitmapFactory.decodeStream(stream));
            } else if (requestCode == 2) {// get image from camera
                newFilePath = data.getStringExtra("filePath");
                File imgFile = new File(newFilePath);
                if (imgFile.exists()) {
                    System.out.println("((((((((((((((((((((((((((((((((((((((((((((" + newFilePath);
                    showDialogOnResult(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
                }
            }
        }
    }

    private void showDialogOnResult(final Bitmap bitmap) {
        //show dialog to save pic with note
        AddTextToPicDialog addTextToPicDialog = new AddTextToPicDialog();
        addTextToPicDialog.showAddTxtDialog(getActivity(), bitmap);
        addTextToPicDialog.setOnSavePicListenerClick(new OnSavePicListenerClick() {
            @Override
            public void saveClicked(String editTextTxt) {
                //todo save image path and txt in data base
                //todo txt should be save in database with pic path if file pah is not null and add it to my list
            }
        });
    }
}