package com.example.beijing_daxing.dialog;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beijing_daxing.adapter.PictureAdapter;
import com.example.beijing_daxing.R;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



/**
 * @author wanghao
 * @date 2023/2/1  17:06
 */
public class TakePictureDialog extends Dialog implements PictureAdapter.ViewOnClickCall {

    RecyclerView pictureRecyclerview;
    private Context context;
    private List<Bitmap> bitmaps;
    private List<File> files;
    private PictureAdapter adapter;
    private dialogReturnListSizeCall call;

    public TakePictureDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        this.context = context;
        adapter = new PictureAdapter(context, this);
    }

    public void setCall(dialogReturnListSizeCall call) {
        this.call = call;
    }

    public void setBitmaps(List<Bitmap> bitmaps, List<File> files) {
        this.bitmaps = bitmaps;
        this.files = files;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_picture);
        pictureRecyclerview = findViewById(R.id.picture_recyclerview);
        findViewById(R.id.confirm_back).setOnClickListener(v -> {
            call.returnListSize(bitmaps.size());
            dismiss();
        });
        // pictureRecyclerview.setLayoutManager(new GridLayoutManager(context, 3));
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
       linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
       pictureRecyclerview.setLayoutManager(linearLayoutManager);
    }


    @Override
    public void show() {
        super.show();
        adapter.setList(bitmaps);
        pictureRecyclerview.setAdapter(adapter);
        Log.e("TAG", "show");
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onclickCall(int position) {
        Log.e("TAG", "在dialog中删除的id:" + position);
        bitmaps.remove(position);
        files.get(position).delete();
        files.remove(position);
        adapter.notifyDataSetChanged();
    }

    public interface dialogReturnListSizeCall {
        public void returnListSize(int size);
    }
}
