package com.fun.originalityprank.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;

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

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.fun.originalityprank.R;
import com.fun.originalityprank.adapter.ThumAdapter;
import com.fun.originalityprank.data.AllData;
import com.fun.originalityprank.data.MydataBase;
import com.fun.originalityprank.databinding.FragmentMainBinding;
import com.fun.originalityprank.tool.Mytools;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class MainFragment extends Fragment {
    FragmentMainBinding binding;
    private List<AllData> allData = new ArrayList<>();
    private List<AllData> maindata = new ArrayList<>();
    private ThumAdapter thumAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentMainBinding.inflate(inflater, container, false);

        initData();

        initEvent();


        binding.mainTitle.setTypeface(Typeface.createFromAsset(requireContext().getAssets(),"WendyOne-Regular.ttf"));

        return binding.getRoot();
    }

    private void initEvent() {
        binding.mainSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDia();
            }
        });
    }

    private void showDia() {
        final Dialog dialog = new Dialog(requireContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
        dialog.setContentView(R.layout.dia_about);
        dialog.setCancelable(true);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        dialog.findViewById(R.id.bt_close).setOnClickListener(v -> dialog.dismiss());

//        dialog.findViewById(R.id.dia_left).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(Intent.ACTION_VIEW);
//                intent.setData(Uri.parse("https://www.baidu.com"));
//                startActivity(intent);
//            }
//        });

        dialog.findViewById(R.id.dia_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("text/plain");
                share.putExtra(Intent.EXTRA_SUBJECT, R.string.app_name);
                String text = getString(R.string.share_title);
                text = text + "\n https://play.google.com/store/apps/details?id=" + "com.fun.originalityprank";
                share.putExtra(Intent.EXTRA_TEXT, text);
                startActivity(Intent.createChooser(share, "choose one"));
            }
        });

        dialog.show();
        dialog.getWindow().setAttributes(lp);
    }

    private void initView(List<AllData> list) {
        thumAdapter = new ThumAdapter(requireActivity(), list);
        binding.marinRv.setAdapter(thumAdapter);
        binding.marinRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
    }

    private void initData() {

        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                allData = MydataBase.getInstance().likeDAO().getAll();
                chekData(allData);
                if (!chekData(allData).isEmpty()) {
                    maindata = chekData(allData);
                    requireActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            initView(maindata);
                        }
                    });
                }

            }
        });


    }

    private static List<AllData> chekData(List<AllData> data) {
        List<AllData> main = new ArrayList<>();
        Set<String> name = new HashSet<>();
        for (AllData mydata : data) {
            if (!name.contains(mydata.getCategoryname())) {
                main.add(mydata);
                name.add(mydata.getCategoryname());
            }
        }
        return main;
    }


}