package com.photo.keyboard.themes.pager;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.photo.keyboard.themes.App;
import com.photo.keyboard.themes.adapter.MainAdapter;
import com.photo.keyboard.themes.databinding.ActivityHomeBinding;
import com.photo.keyboard.themes.listener.MainListener;
import com.photo.keyboard.themes.data.Parent;
import com.photo.keyboard.themes.data.ThemeData;
import com.photo.keyboard.themes.manager.Utils;
import com.photo.keyboard.themes.manager.ConstValue;
import com.photo.keyboard.themes.manager.EnableActive;
import com.photo.keyboard.themes.topon.AdManager;

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

public class MainActivity extends BaseActivity<ActivityHomeBinding> implements MainListener {

    private MainAdapter mainAdapter;


    private DialogEnable dialogStep;
    private ActivityHomeBinding VB;

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

    @Override
    public void onInit() {
        VB = getVb();
        checkPermission();
        AdManager.loadAllAd();
        Utils.INSTANCE.setFont(VB.title);
        initAdapter();
        VB.imCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent setIntent = new Intent(MainActivity.this, LikeActivity.class);
                startActivity(setIntent);
            }
        });
    }


    private void initAdapter() {

        List<Parent> tempList = new ArrayList<>();
        List<Parent> data = App.Companion.getData();
        for (Parent datum : data) {
            Parent parent = new Parent();
            List<ThemeData> list = datum.getList();
            parent.setClassName(datum.getClassName());
            Collections.shuffle(list);
            List<ThemeData> themeData = list.subList(0, 4);
            parent.setList(themeData);
            tempList.add(parent);
        }
        mainAdapter = new MainAdapter(this, tempList);

        LinearLayoutManager linearLayoutManager =
                new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        VB.categoryRecycler.setLayoutManager(linearLayoutManager);
        VB.categoryRecycler.setAdapter(mainAdapter);
        mainAdapter.setListener(this);
    }


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

    @Override
    public void onHomeClickMore(Parent parent) {
        Intent setIntent = new Intent(this, ClassActivity.class);
        setIntent.putExtra(ClassActivity.KEY, parent.getClassName());
        startActivity(setIntent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(dialogStep!= null){
            dialogStep.updateUi();
        }

    }


    private void checkPermission(){
        if (!EnableActive.INSTANCE.toEnable() || !EnableActive.INSTANCE.onsetInput()) {
            if(dialogStep == null){
                dialogStep = new DialogEnable(this);
            }
            if(!dialogStep.isAdded()){
                dialogStep.show(getSupportFragmentManager(),"");
            }
        }
    }
}
