package com.unit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.unit.util.AdvertisingIdClient;
import com.unit.util.ThreadUtil;
import com.unit.util.GlobalUtil;
import com.unit.websdk.LigthWebview;
import com.unit.websdk.WebViewCache;


public class MainActivity extends AppCompatActivity {

    private final static String placementId = "7";
    private final static String appKey = "gL1s3UD25eeMeCWf";

    //google广告id,请通过google sdk动态获取
    private volatile String gaid = "";

    private static String ad_url = "https://h5.gleetap.com/interact/tml?appk={appk}&pid={pid}&did={did}";
    //private static String ad_url = "https://h5.gleetap.com/interact/tml?appk=WEzzDdxRijVbmL1X&pid=15&did={did}";
    private EditText placementIdEditText;
    private EditText appKeyEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GlobalUtil.setMainActivity(this);
        setContentView(R.layout.activity_main);
        placementIdEditText = findViewById(R.id.et_placement_id);
        appKeyEditText = findViewById(R.id.et_appkey);

        final View btn_normal = findViewById(R.id.btn_normal);
        btn_normal.setOnClickListener((v) -> {
            // 正常打开广告链接
            LigthWebview.open(this, getNormalAdUrl());
        });
        final View btn_preload = findViewById(R.id.btn_preload);
        btn_preload.setOnClickListener((v) -> {
            // 打开预加载的广告链接
            LigthWebview.open(this, getPrepareAdUrl());
        });
        btn_normal.setEnabled(false);
        btn_preload.setEnabled(false);

        ThreadUtil.execute(() -> AdvertisingIdClient.getGaid(MainActivity.this, gaid -> {
            MainActivity.this.gaid = gaid;
            ThreadUtil.uiRun(() -> {
                LigthWebview.preload(MainActivity.this, getPrepareAdUrl());
                btn_normal.setEnabled(true);
                btn_preload.setEnabled(true);
            });
        }));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 程序退出之前释放webview缓存列表的资源
        WebViewCache.clear();
        GlobalUtil.setMainActivity(null);
    }

    private String getNormalAdUrl() {
        String url = ad_url.replace("{pid}", getPlacementId()).replace("{appk}", getAppKey());
        return url.replace("{did}", gaid);
    }

    private String getPrepareAdUrl(){
        // 预加载链接测试参数，是为了与不作预加载的链接区分开便于测试。
        String url = ad_url.replace("{pid}", getPlacementId()).replace("{appk}", getAppKey());
        url = url.replace("{did}", gaid);
        return url+"&preload=1";
    }

    private String getPlacementId() {
        String text = placementIdEditText.getText().toString();
        if(TextUtils.isEmpty(text)){
            return placementId;
        }
        return text;
    }

    private String getAppKey(){
        String text = appKeyEditText.getText().toString();
        if(TextUtils.isEmpty(text)){
            return appKey;
        }
        return text.trim();
    }
}