package com.example.myapplication.ui;
import com.example.myapplication.R;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class SearchUI extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        Button backBtn = findViewById(R.id.backBtn);
        Button searchBtn = findViewById(R.id.searchBtn);
        EditText searchInput = findViewById(R.id.searchInput);
        TextView resultsInfo = findViewById(R.id.resultsInfo);
        android.widget.LinearLayout resultsContainer = findViewById(R.id.resultsContainer);

        backBtn.setOnClickListener(v -> finish());
        searchBtn.setOnClickListener(v -> {
            String q = searchInput.getText() == null ? "" : searchInput.getText().toString().trim();
            java.util.List<com.example.myapplication.domain.Note> notes = com.example.myapplication.service.SearchService.search(this, q);
            resultsContainer.removeAllViews();
            if (notes == null || notes.isEmpty()) {
                resultsInfo.setText("未找到匹配的笔记");
                android.widget.TextView empty = new android.widget.TextView(this);
                empty.setText("试试输入章节、科目或专业名称进行搜索");
                empty.setTextColor(android.graphics.Color.parseColor("#666666"));
                resultsContainer.addView(empty);
            } else {
                resultsInfo.setText("共 " + notes.size() + " 条结果");
                int pad = (int) (12 * getResources().getDisplayMetrics().density);
                for (com.example.myapplication.domain.Note n : notes) {
                    android.widget.LinearLayout item = new android.widget.LinearLayout(this);
                    item.setOrientation(android.widget.LinearLayout.VERTICAL);
                    item.setBackgroundColor(android.graphics.Color.WHITE);
                    item.setPadding(pad, pad, pad, pad);

                    android.widget.TextView title = new android.widget.TextView(this);
                    title.setText(n.getTitle() != null ? n.getTitle() : "(无标题)");
                    title.setTextColor(android.graphics.Color.parseColor("#333333"));
                    title.setTypeface(android.graphics.Typeface.DEFAULT_BOLD);

                    android.widget.TextView meta = new android.widget.TextView(this);
                    String created = n.getCreated() == null ? "" : n.getCreated();
                    int likes = n.getLikeCount();
                    meta.setText((created.isEmpty() ? "📅 未知" : "📅 " + created) + "  👍 " + likes);
                    meta.setTextColor(android.graphics.Color.parseColor("#666666"));

                    android.widget.TextView desc = new android.widget.TextView(this);
                    String content = n.getContent();
                    if (content == null) content = "";
                    desc.setText(content.length() > 120 ? content.substring(0, 120) + "..." : content);
                    desc.setTextColor(android.graphics.Color.parseColor("#888888"));

                    item.addView(title);
                    item.addView(meta);
                    item.addView(desc);
                    item.setOnClickListener(v2 -> {
                        com.example.myapplication.service.NoteService.setCurrentNoteId(this, (int) n.getId());
                        android.content.Intent intent = new android.content.Intent(this, com.example.myapplication.ui.NoteDetailUI.class);
                        intent.putExtra("noteId", (int) n.getId());
                        startActivity(intent);
                    });
                    resultsContainer.addView(item);
                }
            }
        });
    }
}