package com.prankapp.fartsounds.airhorn.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.prankapp.fartsounds.airhorn.R;
import com.prankapp.fartsounds.airhorn.adapterdfds.MineLikeAdapter;
import com.prankapp.fartsounds.airhorn.model.PrankListModel;
import com.prankapp.fartsounds.airhorn.utilsfff.Constant;

import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.prankapp.fartsounds.airhorn.utilsfff.SPUtils;

public class MineFragment extends Fragment {
    RecyclerView recyclerView;
    ImageView ivSetting;
    ImageView iv_more;
    RecyclerView rlv_like;
    MineLikeAdapter mineLikeAdapter;
    ArrayList<PrankListModel> pankList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        return view;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iv_more = view.findViewById(R.id.iv_more);
        rlv_like = view.findViewById(R.id.rlv_like);
        iv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),SettingActivity.class));
            }
        });

        mineLikeAdapter = new MineLikeAdapter(R.layout.item_mine_like,pankList);
        rlv_like.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlv_like.setAdapter(mineLikeAdapter);
        mineLikeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), PrankDetailsActivity.class).putExtra("id",pankList.get(position).getCategoryId()));

            }
        });
        mineLikeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.iv_delete){
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("Tips");
                    builder.setMessage("Unfavorited？");
                    builder.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            deleteLike(position);
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 点击取消按钮后的逻辑
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getList();

    }

    public void getList(){
        String shouCang = SPUtils.getInstance().getString(Constant.shouCang);
        pankList.clear();
        if(!shouCang.isEmpty()){
            JSONArray jsonArray = JSONArray.parseArray(shouCang);
            for (int i = 0; i < jsonArray.size(); i++) {
                PrankListModel prankListModel = JSON.parseObject(jsonArray.get(i).toString(), PrankListModel.class);
                pankList.add(prankListModel);
            }
        }

        Log.e("6666size",pankList.size()+"");
        if (pankList.isEmpty()){
            View emptyView = getLayoutInflater().inflate(R.layout.layout_empty_view,null);
            mineLikeAdapter.setEmptyView(emptyView);
        }
        mineLikeAdapter.notifyDataSetChanged();

    }

   public void deleteLike(int index){
       pankList.remove(index);
       Gson gson = new Gson();
       String json = gson.toJson(pankList);
       SPUtils.getInstance().put(Constant.shouCang,json);
       getList();
   }
}
