package com.miraclegarden.smsmessage.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.miraclegarden.library.app.MiracleGardenActivity;
import com.miraclegarden.smsmessage.databinding.ActivityChangePasswordBinding;
import com.miraclegarden.smsmessage.model.ApiError;
import com.miraclegarden.smsmessage.network.ApiService;
import com.miraclegarden.smsmessage.network.TokenManager;

/**
 * 修改密码页面（首登强制修改）
 */
public class ChangePasswordActivity extends MiracleGardenActivity<ActivityChangePasswordBinding> {

    private ApiService apiService;
    private TokenManager tokenManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        apiService = new ApiService(this);
        tokenManager = TokenManager.getInstance(this);

        initView();
    }

    private void initView() {
        binding.btnSubmit.setOnClickListener(v -> attemptChangePassword());
    }

    private void attemptChangePassword() {
        String oldPassword = binding.etOldPassword.getText().toString().trim();
        String newPassword = binding.etNewPassword.getText().toString().trim();
        String confirmPassword = binding.etConfirmPassword.getText().toString().trim();

        if (TextUtils.isEmpty(oldPassword)) {
            binding.etOldPassword.setError("请输入旧密码");
            binding.etOldPassword.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(newPassword)) {
            binding.etNewPassword.setError("请输入新密码");
            binding.etNewPassword.requestFocus();
            return;
        }

        if (newPassword.length() < 6) {
            binding.etNewPassword.setError("新密码至少6位");
            binding.etNewPassword.requestFocus();
            return;
        }

        if (!newPassword.equals(confirmPassword)) {
            binding.etConfirmPassword.setError("两次输入的密码不一致");
            binding.etConfirmPassword.requestFocus();
            return;
        }

        showLoading(true);

        apiService.changePassword(oldPassword, newPassword, new ApiService.ApiCallback<Void>() {
            @Override
            public void onSuccess(Void result) {
                runOnUiThread(() -> {
                    showLoading(false);
                    tokenManager.setFirstLoginComplete();
                    Toast.makeText(ChangePasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    navigateToMain();
                });
            }

            @Override
            public void onError(ApiError error) {
                runOnUiThread(() -> {
                    showLoading(false);
                    Toast.makeText(ChangePasswordActivity.this,
                            "修改失败: " + error.getMessage(), Toast.LENGTH_LONG).show();
                });
            }
        });
    }

    private void showLoading(boolean show) {
        binding.progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
        binding.btnSubmit.setEnabled(!show);
    }

    private void navigateToMain() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        Toast.makeText(this, "请先修改密码", Toast.LENGTH_SHORT).show();
    }
}
