package com.wall.various.wallpaper.ui;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.wall.various.wallpaper.ad.AdManager;
import com.wall.various.wallpaper.ad.onActionListener;
import com.wall.various.wallpaper.databinding.UiWallListBinding;
import com.wall.various.wallpaper.helper.Common;
import com.wall.various.wallpaper.helper.Dbhelper;
import com.wall.various.wallpaper.helper.ItemHelper;
import com.wall.various.wallpaper.inter.ItemClickInterface;
import com.wall.various.wallpaper.myjson.Data;
import com.wall.various.wallpaper.recycler.RecyclerList;

import java.util.List;

public class UiWallList extends AppCompatActivity implements ItemClickInterface {
    private UiWallListBinding vb;

    private String categoryName;
    public static final String key_index = "index";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        vb = UiWallListBinding.inflate(getLayoutInflater());
        setContentView(vb.getRoot());
        Common.initFull(this, false);
        categoryName = getIntent().getStringExtra(key_index);
        vb.name.setText(categoryName);
        initList();
        AdManager.loadAllAd();
        vb.imageviewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdManager.showTopOn(UiWallList.this, new onActionListener() {
                    @Override
                    public void onAction() {
                        finish();
                    }
                });
            }
        });
        AdManager.showTopOn(UiWallList.this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });
    }

    private void initList() {
        List<Data> data = Dbhelper.queryCategoryAll(categoryName);
        ItemHelper itemHelper = new ItemHelper(10, 10, 0);
//        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        vb.list.addItemDecoration(itemHelper);
        vb.list.setLayoutManager(gridLayoutManager);
        int height = Common.dp2Px(180);
        RecyclerList recyclerList = new RecyclerList(this, height, false);
        recyclerList.setDataInfo(data);
        recyclerList.setInfoWallpaperListener(this);
        vb.list.setAdapter(recyclerList);

    }

    @Override
    public void onItemClickAction(Data info) {
        Intent intent = new Intent(this, UiSetWallpaper.class);
        intent.putExtra(Common.key_info, info);
        startActivity(intent);
    }
}