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.ActivityBankEditBinding;
import com.miraclegarden.smsmessage.model.ApiError;
import com.miraclegarden.smsmessage.model.BankInfo;
import com.miraclegarden.smsmessage.network.ApiService;
import com.miraclegarden.smsmessage.network.TokenManager;

public class BankEditActivity extends MiracleGardenActivity<ActivityBankEditBinding> {

    private ApiService apiService;
    private TokenManager tokenManager;
    private String bankId;
    private boolean isEditMode = false;

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

        tokenManager = TokenManager.getInstance(this);
        // 调试用：暂时跳过登录
//        if (!tokenManager.isLoggedIn()) {
//            startActivity(new Intent(this, LoginActivity.class));
//            finish();
//            return;
//        }

        apiService = new ApiService(this);

        initData();
        initView();
    }

    private void initData() {
        bankId = getIntent().getStringExtra("bank_id");
        isEditMode = bankId != null;

        if (isEditMode) {
            binding.tvTitle.setText("编辑银行");
            binding.etBankName.setText(getIntent().getStringExtra("bank_name"));
            binding.etAccount.setText(getIntent().getStringExtra("account"));
            binding.etRemark.setText(getIntent().getStringExtra("remark"));
        } else {
            binding.tvTitle.setText("添加银行");
        }
    }

    private void initView() {
        String username = tokenManager.getUsername();
        if (username != null) {
            binding.tvUsername.setText(username);
        }

        binding.backIv.setOnClickListener(v -> finish());
        binding.btnSave.setOnClickListener(v -> attemptSave());
    }

    private void attemptSave() {
        String bankName = binding.etBankName.getText().toString().trim();
        String account = binding.etAccount.getText().toString().trim();
        String remark = binding.etRemark.getText().toString().trim();

        if (TextUtils.isEmpty(bankName)) {
            binding.etBankName.setError("请输入银行名称");
            binding.etBankName.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(account)) {
            binding.etAccount.setError("请输入账户");
            binding.etAccount.requestFocus();
            return;
        }

        BankInfo bankInfo = new BankInfo(bankName, account, remark);

        showLoading(true);

        if (isEditMode) {
            apiService.updateBank(bankId, bankInfo, new ApiService.ApiCallback<BankInfo>() {
                @Override
                public void onSuccess(BankInfo result) {
                    runOnUiThread(() -> {
                        showLoading(false);
                        Toast.makeText(BankEditActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        finish();
                    });
                }

                @Override
                public void onError(ApiError error) {
                    runOnUiThread(() -> {
                        showLoading(false);
                        Toast.makeText(BankEditActivity.this,
                                "修改失败: " + error.getMessage(), Toast.LENGTH_LONG).show();
                    });
                }
            });
        } else {
            apiService.createBank(bankInfo, new ApiService.ApiCallback<BankInfo>() {
                @Override
                public void onSuccess(BankInfo result) {
                    runOnUiThread(() -> {
                        showLoading(false);
                        Toast.makeText(BankEditActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                        finish();
                    });
                }

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

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