package uni.deepoil.com.ddauth;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.dingtalk.openauth.utils.DDAuthConstant;

import uni.deepoil.com.DingTalkModule;

/**
 * @author Berial
 * @since 2025-06-09
 */
public class DDAuthActivity extends AppCompatActivity {

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

        Intent intent = getIntent();
        String authCode = intent.getStringExtra(DDAuthConstant.CALLBACK_EXTRA_AUTH_CODE);
        String state = intent.getStringExtra(DDAuthConstant.CALLBACK_EXTRA_STATE);
        String error = intent.getStringExtra(DDAuthConstant.CALLBACK_EXTRA_ERROR);

        Log.d("DingTalk", "authCode: " + authCode + " state: " + state + " error: " + error);

        if (!TextUtils.isEmpty(authCode)) {
            // 授权成功
            Message message = DingTalkModule.handler.obtainMessage(1, authCode);
            message.sendToTarget();
        } else {
            // 授权失败
            Message message = DingTalkModule.handler.obtainMessage(2, error);
            message.sendToTarget();
        }
        finish();
    }
}
