package com.key.simplekeyboard.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.key.simplekeyboard.R;
import com.key.simplekeyboard.SimpleKeyboardSkins;
import com.key.simplekeyboard.activity.AllActivity;
import com.key.simplekeyboard.activity.PreviewActivity;
import com.key.simplekeyboard.activity.SearchActivity;
import com.key.simplekeyboard.adapter.ThumAdapter;
import com.key.simplekeyboard.data.MydataBase;
import com.key.simplekeyboard.data.Otherdata;
import com.key.simplekeyboard.tools.Mytools;
import com.key.simplekeyboard.tools.SpaceItem;
import com.key.simplekeyboard.tools.StaticValue;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.AlphaPageTransformer;
import com.youth.banner.transformer.DepthPageTransformer;
import com.youth.banner.transformer.RotateUpPageTransformer;

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


public class MainFragment extends Fragment {
    private ImageView search;
    private LinearLayout viewall;
    private RecyclerView mainrv;
    private ThumAdapter thumAdapter;
    private TextView title;
    private Banner banner;
    private List<Otherdata> randomOriginals = new ArrayList<>();
    private List<Otherdata> mainrandomlist = new ArrayList<>();

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        search = view.findViewById(R.id.main_search);
        viewall = view.findViewById(R.id.view_all);
        mainrv = view.findViewById(R.id.main_rv);
        banner = view.findViewById(R.id.banner);
        title = view.findViewById(R.id.main_title);
        title.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "DenkOne-Regular.ttf"));
        List<Otherdata> otherdataList = generateRandomOtherdata(0, 5);
        for (Otherdata data : otherdataList) {
            if (randomOriginals.size() < 5) {
                randomOriginals.add(data);
            }
        }

        List<Otherdata> othList = generateRandomOtherdata(0, 20);
        for (Otherdata data : othList) {
            if (mainrandomlist.size() < 20) {
                mainrandomlist.add(data);
            }
        }
        banner.setAdapter(new BannerImageAdapter<Otherdata>(randomOriginals) {
            @Override
            public void onBindView(BannerImageHolder holder, Otherdata data, int position, int size) {
                ImageView imageView = holder.imageView;
                Glide.with(requireContext())
                        .load(data.getPreview())
//                        .placeholder(R.mipmap.icon_logo)
                        .into(imageView);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            }
        });
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                Otherdata otherdata = randomOriginals.get(position);
                String url = otherdata.getZipUrl();
                String pre = otherdata.getPreview();
                String name = otherdata.getTitle();
                Intent intent = new Intent(requireContext(), PreviewActivity.class);
                intent.putExtra(StaticValue.KEY_PRE, pre);
                intent.putExtra(StaticValue.KEY_URL, url);
                intent.putExtra(StaticValue.KEY_NAME, name);
                requireContext().startActivity(intent);
            }
        });
        banner.setIndicator(new CircleIndicator(requireContext()));
        banner.setBannerRound(16);
        banner.setIndicatorRadius(100);


        thumAdapter = new ThumAdapter(requireContext(), mainrandomlist);
        mainrv.setAdapter(thumAdapter);
        mainrv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        SpaceItem spaceItem = new SpaceItem(12, 12, 12);
        mainrv.addItemDecoration(spaceItem);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
        viewall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AllActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }

    public static List<Otherdata> generateRandomOtherdata(int star, int end) {
        List<Otherdata> allOtherdata = SimpleKeyboardSkins.getOtherdata();
        Collections.shuffle(allOtherdata);
        return allOtherdata.subList(star, end);
    }

}