package com.photo.keyboard.themes.pager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

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

import com.photo.keyboard.themes.adapter.ClassAdapter;
import com.photo.keyboard.themes.databinding.ActivityLikeBinding;
import com.photo.keyboard.themes.listener.MainListener;
import com.photo.keyboard.themes.data.ThemeData;
import com.photo.keyboard.themes.manager.Utils;
import com.photo.keyboard.themes.manager.DbManager;
import com.photo.keyboard.themes.manager.ItemSpace;
import com.photo.keyboard.themes.manager.ConstValue;
import com.photo.keyboard.themes.topon.AdManager;
import com.photo.keyboard.themes.topon.onActionListener;

import java.util.List;

public class LikeActivity extends BaseActivity<ActivityLikeBinding> implements View.OnClickListener, MainListener {

    private ActivityLikeBinding bind;
    private ClassAdapter ClassAdapter;

    @NonNull
    @Override
    public ActivityLikeBinding returnViewbinding() {
        Utils.INSTANCE.initFull(this, false);
        return ActivityLikeBinding.inflate(getLayoutInflater());
    }

    @Override
    public void onInit() {
        bind = getVb();
        AdManager.loadAllAd();
        Utils.INSTANCE.setFont(bind.idTheme);
        bind.idBack.setOnClickListener(this);
        AdManager.showTopOn(LikeActivity.this, new onActionListener() {
            @Override
            public void onAction() {

            }
        });
        update();

    }

    @Override
    protected void onResume() {
        super.onResume();
        update();
    }

    private void update() {
        bind.loadingPb.setVisibility(View.VISIBLE);
        List<ThemeData> themeData = DbManager.searchAllLike();
        if (themeData.size() == 0) {
            bind.loadingPb.setVisibility(View.GONE);
            bind.recycler.setVisibility(View.GONE);
            bind.emptyLayout.setVisibility(View.VISIBLE);
        } else {
            initAdapter(themeData);
            bind.loadingPb.setVisibility(View.GONE);
            bind.recycler.setVisibility(View.VISIBLE);
            bind.emptyLayout.setVisibility(View.GONE);
        }
    }

    private void initAdapter(List<ThemeData> themeData) {
        if (ClassAdapter == null) {
            ClassAdapter = new ClassAdapter(this, themeData);
            ClassAdapter.setListener(this);
            ItemSpace itemSpace = new ItemSpace(Utils.INSTANCE.dpToUnitPx(8), Utils.INSTANCE.dpToUnitPx(8), Utils.INSTANCE.dpToUnitPx(8));
            GridLayoutManager gridLayoutManager =
                    new GridLayoutManager(this, 2);
            bind.recycler.setLayoutManager(gridLayoutManager);
            bind.recycler.setAdapter(ClassAdapter);
            bind.recycler.addItemDecoration(itemSpace);
        }else {
            ClassAdapter.updateData(themeData);
        }


    }

    @Override
    public void onClick(View v) {
        if (v.equals(bind.idBack)) {
            finish();
        }
    }

    @Override
    public void onHomeClick(ThemeData themeData) {
        Intent setIntent = new Intent(this, SetThemesActivity.class);
        setIntent.putExtra(ConstValue.KEYTheme, themeData);
        startActivity(setIntent);
    }


}