package com.example.myapplication;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.DataBase.DatabaseHelper;

public class AdminActivity extends AppCompatActivity {
    private ListView lvUsers;
    private DatabaseHelper dbHelper;
    private SimpleCursorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        dbHelper = new DatabaseHelper(this);
        lvUsers = findViewById(R.id.lvUsers);
        Button btnBack = findViewById(R.id.btnBack);

        loadUsers();

        btnBack.setOnClickListener(v -> finish());
    }

    private void loadUsers() {
        Cursor cursor = dbHelper.getAllUsers();

        String[] from = new String[]{
                DatabaseHelper.COLUMN_USERNAME,
                DatabaseHelper.COLUMN_PASSWORD,
                DatabaseHelper.COLUMN_USER_PROJECT_NAME,
                DatabaseHelper.COLUMN_USER_PROJECT_ID
        };

        int[] to = new int[]{
                R.id.tvUsername,
                R.id.tvPassword,
                R.id.tvProjectName,
                R.id.tvProjectId
        };

        adapter = new SimpleCursorAdapter(this, R.layout.user_item,
                cursor, from, to, 0) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);

                // 获取当前行的数据
                Cursor cursor = (Cursor) getItem(position);
                @SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_USERNAME));

                // 设置删除按钮点击事件
                Button btnDelete = view.findViewById(R.id.btnDelete);
                btnDelete.setOnClickListener(v -> {
                    if (dbHelper.deleteUser(username)) {
                        Toast.makeText(AdminActivity.this, "用户已删除", Toast.LENGTH_SHORT).show();
                        // 重新加载数据
                        Cursor newCursor = dbHelper.getAllUsers();
                        changeCursor(newCursor);
                    } else {
                        Toast.makeText(AdminActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                    }
                });
                TextView tvProjectName = view.findViewById(R.id.tvProjectName);
                tvProjectName.setOnClickListener(v -> {
                    if (tvProjectName.getMaxLines() == 1) {
                        tvProjectName.setMaxLines(Integer.MAX_VALUE); // 展开
                    } else {
                        tvProjectName.setMaxLines(1); // 收起
                    }
                });
                return view;
            }
        };

        lvUsers.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        dbHelper.close();
        super.onDestroy();
    }
}