package com.example.myapplication.ui;

import androidx.appcompat.app.AppCompatActivity;

public class NoteEditUI extends AppCompatActivity {
    private long noteId;

    @Override
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.example.myapplication.R.layout.activity_note_edit);

        noteId = getIntent().getLongExtra("noteId", -1);
        if (noteId <= 0) {
            int nid = com.example.myapplication.service.NoteService.getCurrentNoteId(this);
            noteId = nid;
        }
        android.widget.EditText titleEt = findViewById(com.example.myapplication.R.id.editNoteTitle);
        android.widget.EditText contentEt = findViewById(com.example.myapplication.R.id.editNoteContent);
        android.widget.Button saveBtn = findViewById(com.example.myapplication.R.id.saveNoteBtn);
        android.widget.Button cancelBtn = findViewById(com.example.myapplication.R.id.cancelEditBtn);
        android.widget.Button deleteBtn = findViewById(com.example.myapplication.R.id.deleteNoteBtn);

        if (noteId > 0) {
            com.example.myapplication.repository.NoteRepository repo = new com.example.myapplication.repository.NoteRepository(this);
            com.example.myapplication.domain.Note note = repo.getNoteById(noteId);
            if (note != null) {
                if (titleEt != null) titleEt.setText(note.getTitle() != null ? note.getTitle() : "");
                if (contentEt != null) contentEt.setText(note.getContent() != null ? note.getContent() : "");
            }
        }

        saveBtn.setOnClickListener(v -> {
            String title = titleEt.getText().toString().trim();
            String content = contentEt.getText().toString().trim();
            if (title.isEmpty()) {
                com.example.myapplication.Utils.toast(this, "标题不能为空");
                return;
            }
            // 仅允许上传者编辑
            com.example.myapplication.repository.NoteRepository repo0 = new com.example.myapplication.repository.NoteRepository(this);
            com.example.myapplication.domain.Note n0 = repo0.getNoteById(noteId);
            String currentUser = com.example.myapplication.service.AuthService.getCurrentUser(this);
            if (n0 != null && n0.getUploaderId() != null && !n0.getUploaderId().equals(currentUser)) {
                com.example.myapplication.Utils.toast(this, "仅作者可编辑");
                return;
            }
            android.content.ContentValues cv = new android.content.ContentValues();
            cv.put("title", title);
            cv.put("content", content);
            boolean ok = new com.example.myapplication.repository.NoteRepository(this).updateNote(noteId, cv);
            com.example.myapplication.Utils.toast(this, ok ? "保存成功" : "保存失败");
            if (ok) finish();
        });

        cancelBtn.setOnClickListener(v -> finish());

        deleteBtn.setOnClickListener(v -> {
            android.app.AlertDialog.Builder b = new android.app.AlertDialog.Builder(this);
            b.setTitle("删除笔记");
            b.setMessage("确定删除这条笔记吗？");
            b.setPositiveButton("删除", (d, which) -> {
                com.example.myapplication.repository.NoteRepository repo0 = new com.example.myapplication.repository.NoteRepository(this);
                com.example.myapplication.domain.Note n0 = repo0.getNoteById(noteId);
                String currentUser = com.example.myapplication.service.AuthService.getCurrentUser(this);
                if (n0 != null && n0.getUploaderId() != null && !n0.getUploaderId().equals(currentUser)) {
                    com.example.myapplication.Utils.toast(this, "仅作者可删除");
                    return;
                }
                boolean ok = new com.example.myapplication.repository.NoteRepository(this).deleteNote(noteId);
                com.example.myapplication.Utils.toast(this, ok ? "已删除" : "删除失败");
                if (ok) finish();
            });
            b.setNegativeButton("取消", (d, which) -> {});
            b.show();
        });
    }
}