package com.example.sleep;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.sleep.database.dao.DatabaseManager;

import java.io.ByteArrayOutputStream;

public class EditScreen extends FragmentActivity {
    private ImageView showImage;
    private ImageView postImage;
    private String imageUrl;
    private EditText messageEdit;
    private Bitmap bitmap;

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

        Bundle extras = getIntent().getExtras();
        boolean isPhoto = getIntent().getBooleanExtra("isPhoto", false);

        postImage = findViewById(R.id.event_content_post);
        messageEdit = findViewById(R.id.event_content);
        showImage = findViewById(R.id.event_image);

        // 根据传入的数据决定是否显示图片
        if (isPhoto) {
            imageUrl = getIntent().getStringExtra("bitmapImageString");
            Bitmap bitmap = BitmapFactory.decodeFile(imageUrl);
            showImage.setImageBitmap(bitmap);
        } else {
            if (extras != null) {
                bitmap = extras.getParcelable("bitmapImage");
                if (bitmap != null) {
                    showImage.setImageBitmap(bitmap);
                }
            }
        }

        postImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = messageEdit.getText().toString();
                if (TextUtils.isEmpty(message)) {
                    Toast.makeText(EditScreen.this, "请添加消息", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 从SharedPreferences获取用户名
                SharedPreferences sharedPreferences = EditScreen.this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
                String retrievedUserName = sharedPreferences.getString("UserNameKey", "DefaultUserName");

                // 打开数据库管理器
                DatabaseManager manager = new DatabaseManager(EditScreen.this);
                manager.open();

                // 根据是否是照片贴文，将消息和图片信息保存到数据库
                if (isPhoto) {
                    manager.postMood(retrievedUserName, message, imageUrl);
                } else {
                    String bitmapUrl = bitmapToBase64(bitmap);
                    manager.postMood(retrievedUserName, message, bitmapUrl);
                }

                // 关闭数据库管理器
                manager.close();

                finish();
            }
        });
    }

    // 将Bitmap转换为Base64编码的字符串
    public String bitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
}
