package com.kaixed.kchat.view.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EdgeEffect;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kaixed.kchat.databinding.ActivitySearchBinding;
import com.kaixed.kchat.model.search.SearchItem;
import com.kaixed.kchat.view.adapter.SearchResultAdapter;

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


public class SearchActivity extends AppCompatActivity {

    private ActivitySearchBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivitySearchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setOnClickListener();

        setupRecycleView();


        binding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setupViewVisibility(s.length());

            }
        });

    }

    private void setupViewVisibility(int length) {



        binding.rvSearchResult.setVisibility(length > 0 ? View.VISIBLE : View.INVISIBLE);
        binding.tvPageSetting.setVisibility(length > 0 ? View.INVISIBLE : View.VISIBLE);
    }

    private void setupRecycleView() {
        binding.rvSearchResult.setEdgeEffectFactory(new RecyclerView.EdgeEffectFactory() {
            @NonNull
            @Override
            protected EdgeEffect createEdgeEffect(@NonNull RecyclerView view, int direction) {
                return new EdgeEffect(view.getContext()) {
                    @Override
                    public void onPull(float deltaDistance, float displacement) {
                        // 禁止下拉时的效果
                    }

                    @Override
                    public void onRelease() {
                        // 禁止释放时的效果
                    }

                    @Override
                    public void onAbsorb(int velocity) {
                        // 禁止吸收时的效果
                    }
                };
            }
        });

        SearchResultAdapter searchResultAdapter = getSearchResultAdapter();
        binding.rvSearchResult.setAdapter(searchResultAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        binding.rvSearchResult.setLayoutManager(layoutManager);
    }

    private void setOnClickListener() {
        binding.tvCancel.setOnClickListener(v -> {
            finish();
        });
    }

    private static @NonNull SearchResultAdapter getSearchResultAdapter() {
        SearchItem contactItem = new SearchItem("sa", "kaixed", "as", false, "联系人");
        SearchItem chatHistoryItem = new SearchItem("sa", "kaixed", "as", false, "群聊");
        SearchItem groupItem = new SearchItem("sa", "kaixed", "as", false, "聊天记录");

        SearchItem searchItem1 = new SearchItem("sa", "kaixed", "as", true, "联系人");
        SearchItem searchItem2 = new SearchItem("sa", "kaixed", "as", true, "群聊");
        SearchItem searchItem3 = new SearchItem("sa", "kaixed", "as", true, "聊天记录");


        List<Object> objects = new ArrayList<>(12);
        objects.add("联系人");
        objects.add(contactItem);
        objects.add(contactItem);
        objects.add(searchItem1);

        objects.add("群聊");
        objects.add(groupItem);
        objects.add(groupItem);
        objects.add(searchItem2);

        objects.add("聊天记录");
        objects.add(chatHistoryItem);
        objects.add(chatHistoryItem);
        objects.add(searchItem3);


        SearchResultAdapter searchResultAdapter = new SearchResultAdapter(objects);
        return searchResultAdapter;
    }
}