package com.example.sleep.database.dao;

import static com.example.sleep.database.MyDataBaseHelper.CONTENT;
import static com.example.sleep.database.MyDataBaseHelper.IMAGE_URL;
import static com.example.sleep.database.MyDataBaseHelper.TIMESTAMP;
import static com.example.sleep.database.MyDataBaseHelper.USER_NAME;

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

import com.example.sleep.ItemModel;
import com.example.sleep.database.MyDataBaseHelper;

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

public class DatabaseManager {
    private MyDataBaseHelper dbHelper;
    private SQLiteDatabase database;

    public DatabaseManager(Context context) {
        dbHelper = new MyDataBaseHelper(context);
    }

    public void open() {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    // A用户发表心情
    public long postMood(String userName, String content, String imageUrl) {
        ContentValues values = new ContentValues();
        values.put(USER_NAME, userName);
        values.put(CONTENT, content);
        values.put(IMAGE_URL, imageUrl);

        return database.insert(MyDataBaseHelper.TABLE_STATUS, null, values);
    }

    public List<ItemModel> getUserNameEvent(String userName) {
        // 使用参数化查询以防止SQL注入
        String selection = USER_NAME + " = ?";
        String[] selectionArgs = {userName};
        List<ItemModel> dataList = new ArrayList<>();
        Cursor cursor = database.query(MyDataBaseHelper.TABLE_STATUS, null, selection, selectionArgs, null, null, TIMESTAMP + " DESC");

        while (cursor.moveToNext()) {
            String text = cursor.getString(cursor.getColumnIndex(CONTENT));
            String imageUrl = cursor.getString(cursor.getColumnIndex(IMAGE_URL));

            // 根据你的ItemModel的构造函数来适当地修改下一行
            dataList.add(new ItemModel(imageUrl, text));
        }

        cursor.close();
        database.close();
        return dataList;
    }

    // B用户评论A用户的心情
    public long commentMood(long statusId, String commenter, String comment) {
        ContentValues values = new ContentValues();
        values.put(MyDataBaseHelper.STATUS_ID, statusId);
        values.put(MyDataBaseHelper.COMMENTER, commenter);
        values.put(MyDataBaseHelper.COMMENT, comment);

        return database.insert(MyDataBaseHelper.TABLE_COMMENTS, null, values);
    }

    // B用户查看A用户的心情和评论
    public Cursor viewMoodAndComments(long statusId) {
        String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
                "LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
                "ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
                " WHERE s." + MyDataBaseHelper.STATUS_ID + " = ?";

        return database.rawQuery(query, new String[]{String.valueOf(statusId)});
    }

    // A用户查看自己的心情和B用户的评论
    public Cursor viewOwnMoodAndComments(String userName) {
        String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
                "LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
                "ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
                " WHERE s." + USER_NAME + " = ?";

        return database.rawQuery(query, new String[]{userName});
    }


    // 插入一条心情动态
    public long insertStatus(String userName, String content, String imageUrl) {
        ContentValues values = new ContentValues();
        values.put(USER_NAME, userName);
        values.put(CONTENT, content);
        values.put(IMAGE_URL, imageUrl);

        return database.insert(MyDataBaseHelper.TABLE_STATUS, null, values);
    }

    // 插入一条评论
    public long insertComment(long statusId, String commenter, String comment) {
        ContentValues values = new ContentValues();
        values.put(MyDataBaseHelper.STATUS_ID, statusId);
        values.put(MyDataBaseHelper.COMMENTER, commenter);
        values.put(MyDataBaseHelper.COMMENT, comment);

        return database.insert(MyDataBaseHelper.TABLE_COMMENTS, null, values);
    }

    public Cursor getStatusWithComments(long statusId) {
        String query = "SELECT * FROM " + MyDataBaseHelper.TABLE_STATUS + " s " +
                "LEFT JOIN " + MyDataBaseHelper.TABLE_COMMENTS + " c " +
                "ON s." + MyDataBaseHelper.STATUS_ID + " = c." + MyDataBaseHelper.STATUS_ID +
                " WHERE s." + MyDataBaseHelper.STATUS_ID + " = ?";

        return database.rawQuery(query, new String[]{String.valueOf(statusId)});
    }
}