package com.tsi.navi.activity.dialog;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SearchView;

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

import com.tsi.navi.R;
import com.tsi.navi.TsiNaviApplication;
import com.tsi.navi.activity.adapter.NodeCrossItemAdapter;
import com.tsi.navi.activity.model.NodeCrossItem;
import com.tsi.navi.vo.NodeVo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;

public class NodeCrossDialog extends BaseDialog {
    private static final String TAG = NodeCrossDialog.class.getSimpleName();

    private RecyclerView recyclerView;
    private NodeCrossItemAdapter nodeCrossAdapter;
    private ArrayList<NodeCrossItem> nodeCrossItems = new ArrayList<>();
    private NodeCrossItem selectedNode;

    class NodeCrossItemComparator implements Comparator<NodeCrossItem> {
        @Override
        public int compare(NodeCrossItem f1, NodeCrossItem f2) {
            return f1.getName().compareTo(f2.getName());
        }
    }

    public NodeCrossDialog(@NonNull Context context, @NonNull IDialogClickListener clickListener) {
        super(context, clickListener);
        this.selectedNode = null;
    }

    public NodeCrossItem getSelectedNode() {
        return this.selectedNode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_node_cross);
        setupButton(R.string.dialog_title_node_cross, false);

        for (Map.Entry<Long, NodeVo> obj : TsiNaviApplication.getNodeMap().entrySet()) {
            if (obj.getValue().getNodeId() >= 1570062500 &&
                    obj.getValue().getNodeId() <= 1570300000) {
                if (obj.getValue().isReal()) {
                    this.nodeCrossItems.add(NodeCrossItem.builder()
                            .nodeId(obj.getValue().getNodeId())
                            .name(obj.getValue().getName())
                            //.lat(obj.getValue().getLat())
                            //.lng(obj.getValue().getLng())
                            .type(obj.getValue().getType())
                            .online(obj.getValue().isOnline())
                            .build());
                }
            }
        }
        Collections.sort(this.nodeCrossItems, new NodeCrossItemComparator());

        this.nodeCrossAdapter = new NodeCrossItemAdapter(context, this.nodeCrossItems);
        this.recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        this.recyclerView.setAdapter(this.nodeCrossAdapter);
        this.recyclerView.setLayoutManager(new LinearLayoutManager(context));
        //this.recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL,false));

        this.nodeCrossAdapter.setOnItemClickListener(new NodeCrossItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int pos, NodeCrossItem item) {
                if (item != null) {
                    selectedNode = item;
                    dismiss();
                }
            }
        });

        SearchView svSearch = (SearchView)findViewById(R.id.sv_search);
        svSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Log.e(TAG, "query: " + query);
                nodeCrossAdapter.getFilter().filter(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Log.e(TAG, "newText: " + newText);
                nodeCrossAdapter.getFilter().filter(newText);
                return false;
            }
        });
    }
}
