package com.addd.xingdongli.data;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

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

public class UserRepository {

    private DBHelper dbHelper;

    // 构造函数，初始化 DBHelper 对象
    public UserRepository(Context context) {
        dbHelper = new DBHelper(context);
    }

    /**
     * 插入一个新用户
     *
     * @param user 要插入的用户对象
     * @return 返回新记录的行ID，如果插入失败返回-1
     */
    public long insertUser(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", user.getName());
        values.put("gender", user.getGender());
        values.put("age", user.getAge());
        values.put("height", user.getHeight());
        values.put("weight", user.getWeight());
        values.put("goal", user.getGoal());
        values.put("created_at", user.getCreatedAt());
        long id = db.insert("user_info", null, values);
        //db.close();
        return id;
    }

    /**
     * 查询所有用户
     *
     * @return 返回一个包含所有用户的列表
     */
    public List<User> getAllUsers() {
        List<User> userList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM user_info", null);
        if (cursor.moveToFirst()) {
            do {
                User user = new User();
                user.setId(cursor.getInt(cursor.getColumnIndexOrThrow("id")));
                user.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
                user.setGender(cursor.getString(cursor.getColumnIndexOrThrow("gender")));
                user.setAge(cursor.getInt(cursor.getColumnIndexOrThrow("age")));
                user.setHeight(cursor.getFloat(cursor.getColumnIndexOrThrow("height")));
                user.setWeight(cursor.getFloat(cursor.getColumnIndexOrThrow("weight")));
                user.setGoal(cursor.getString(cursor.getColumnIndexOrThrow("goal")));
                user.setCreatedAt(cursor.getString(cursor.getColumnIndexOrThrow("created_at")));
                userList.add(user);
            } while (cursor.moveToNext());
        }
        cursor.close();
        //db.close();
        return userList;
    }

    /**
     * 更新用户信息
     *
     * @param user 包含更新内容的用户对象（必须包含正确的 id）
     * @return 返回更新的行数
     */
    public int updateUser(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", user.getName());
        values.put("gender", user.getGender());
        values.put("age", user.getAge());
        values.put("height", user.getHeight());
        values.put("weight", user.getWeight());
        values.put("goal", user.getGoal());
        values.put("created_at", user.getCreatedAt());
        int rows = db.update("user_info", values, "id = ?", new String[]{String.valueOf(user.getId())});
        //db.close();
        return rows;
    }

    /**
     * 删除指定用户
     *
     * @param userId 要删除的用户ID
     * @return 返回被删除的行数
     */
    public int deleteUser(int userId) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int rows = db.delete("user_info", "id = ?", new String[]{String.valueOf(userId)});
        //db.close();
        return rows;
    }
}
