package com.ar.imagetracear.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;


import com.ar.imagetracear.R;
import com.ar.imagetracear.databinding.ActivitySublistBinding;
import com.ar.imagetracear.database.AppDatabase;
import com.ar.imagetracear.database.ImageTraceEntry;
import com.ar.imagetracear.adapter.SubListAdapter;
import com.ar.imagetracear.topon.AdManager;
import com.ar.imagetracear.topon.onActionListener;
import com.ar.imagetracear.utils.ItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class SubListActivity extends AppCompatActivity {
    private ActivitySublistBinding bindingRef;
    private String categoryKey;
    private String categoryTitle;
    private SubListAdapter imageListAdapter;

    @Override
    protected void onCreate(Bundle state) {
        super.onCreate(state);

        EdgeToEdge.enable(this);

        bindingRef = ActivitySublistBinding.inflate(getLayoutInflater());
        setContentView(bindingRef.getRoot());

        View mainLayout = findViewById(R.id.main);
        ViewCompat.setOnApplyWindowInsetsListener(mainLayout, (view, windowInsets) -> {
            Insets bars = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
            view.setPadding(bars.left, bars.top, bars.right, bars.bottom);
            return windowInsets;
        });

        AdManager.loadAllAd();
        AdManager.showTopOn(this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });

        readIntentData();
        initializeViews();
        prepareListView();
        loadCategoryImages();
    }

    private void readIntentData() {
        Intent incoming = getIntent();
        categoryKey = incoming.getStringExtra("Original");
        categoryTitle = incoming.getStringExtra("Title");
    }

    private void initializeViews() {
        setupHeaderTitle();
        bindActions();
    }

    private void setupHeaderTitle() {
        bindingRef.title.setText(categoryTitle);
    }

    private void bindActions() {
        bindingRef.back.setOnClickListener(v -> {
            AdManager.showTopOn(this, new onActionListener() {
                @Override
                public void onAction() {
                    finish();
                }
            });
        });
    }

    private void prepareListView() {
        bindingRef.recyclerview.setLayoutManager(new GridLayoutManager(this, 2));
        imageListAdapter = new SubListAdapter(this, new ArrayList<>());
        bindingRef.recyclerview.setAdapter(imageListAdapter);
        bindingRef.recyclerview.addItemDecoration(new ItemDecoration(16, 19, 10));
    }

    private void loadCategoryImages() {
        AppDatabase db = AppDatabase.getInstance(this);
        db.imageTraceEntryDao().getFileByPath(categoryKey).observe(this, new Observer<List<ImageTraceEntry>>() {
            @Override
            public void onChanged(List<ImageTraceEntry> result) {
                if (result != null) {
                    imageListAdapter.updateEntries(result);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bindingRef = null;
    }
}