package com.github.material.monitor.demo.mock;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.material.monitor.MaterialStyle;
import com.github.material.monitor.demo.R;

/**
 * 模拟第三方 SDK 全屏插屏 / 激励 Activity。
 *
 * <p><b>本类不含任何 MaterialMonitor 调用</b>——截帧、上报、{@code onAdClosed} 均由宿主
 * {@link com.github.material.monitor.demo.MainActivity} 负责（对接文档 §5.4、§5.5）。</p>
 */
public final class MockAdActivity extends Activity implements View.OnClickListener {

    public static final String EXTRA_STYLE = "extra_style";
    public static final String EXTRA_DISPLAY_MS = "extra_display_ms";

    private final Handler mainHandler = new Handler(Looper.getMainLooper());
    private MaterialStyle style;
    private long displayMs;

    private final Runnable autoCloseRunnable = new Runnable() {
        @Override
        public void run() {
            closeAdPage();
        }
    };

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

        style = MaterialStyle.values()[getIntent().getIntExtra(EXTRA_STYLE, MaterialStyle.INTERSTITIAL.ordinal())];
        displayMs = getIntent().getLongExtra(EXTRA_DISPLAY_MS, 4000L);
        if (displayMs < 1000L) {
            displayMs = 1000L;
        }

        View root = findViewById(R.id.mock_fullscreen_root);
        TextView titleView = findViewById(R.id.tv_mock_ad_title);
        TextView hintView = findViewById(R.id.tv_mock_ad_hint);
        Button closeBtn = findViewById(R.id.btn_close_ad);

        if (style == MaterialStyle.REWARDED_VIDEO) {
            root.setBackgroundColor(Color.parseColor("#D8D5FF"));
            titleView.setText(R.string.mock_rewarded_title);
            hintView.setText(R.string.mock_rewarded_hint);
        } else {
            root.setBackgroundColor(Color.parseColor("#FFE1C6"));
            titleView.setText(R.string.mock_interstitial_title);
            hintView.setText(R.string.mock_interstitial_hint);
        }

        closeBtn.setOnClickListener(this);
        mainHandler.postDelayed(autoCloseRunnable, displayMs);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_close_ad) {
            closeAdPage();
        }
    }

    private void closeAdPage() {
        if (isFinishing()) {
            return;
        }
        mainHandler.removeCallbacks(autoCloseRunnable);
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onBackPressed() {
        closeAdPage();
    }

    @Override
    protected void onDestroy() {
        mainHandler.removeCallbacks(autoCloseRunnable);
        super.onDestroy();
    }

    /** 由宿主构造 Intent，仅传递 UI 展示所需参数 */
    public static Intent buildIntent(Activity host, MaterialStyle style, long displayMs) {
        Intent intent = new Intent(host, MockAdActivity.class);
        intent.putExtra(EXTRA_STYLE, style.ordinal());
        intent.putExtra(EXTRA_DISPLAY_MS, displayMs);
        return intent;
    }
}
