package com.yutou.jianr_mg.views;

import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
import com.qq.e.ads.nativ.NativeExpressADView;
import com.qq.e.comm.util.AdError;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.Network.HttpApi;
import com.yutou.jianr_mg.Network.HttpUtils;
import com.yutou.jianr_mg.Tools.AdTools;
import com.yutou.jianr_mg.Tools.Utils;

import org.json.JSONObject;

import java.util.List;

public class NativeAdActivity extends AppCompatActivity {
    private LinearLayout layout;
    private KProgressHUD hud;
    private Handler handler;
    private String appid,posId;
    private int index=0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout=new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);
        handler=new Handler();
        hud= Utils.showLoading(this,null,null);
        hud.show();
        init();
    }
    public void init() {
        try {
            JSONObject json = new JSONObject();
            json.put("type", 4);
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.AD_GET, json,new HttpInterface() {
                @Override
                public void httpGetData(String string, int state) {
                    try {
                        JSONObject json = new JSONObject(string.toString());
                        appid = json.getJSONObject("data").getString("appid");
                        posId = json.getJSONObject("data").getString("posid");
                        handler.post(()->initAd());
                    } catch (Exception e) {
                        e.printStackTrace();
                        Utils.toast(null,"今天恰饱了");
                        finish();
                    }

                }

                @Override
                public void httpError(Exception e) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initAd() {
        if (appid == null || posId == null) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    initAd();
                }
            }, 300);
            return;
        }
        TextView textView=new TextView(this);
        textView.setText("点击任意广告均可获取2点积分");
        layout.addView(textView);
        startAd();
    }
    private void startAd() {
        AdTools.upload( AdTools.Native, AdTools.req);
        NativeExpressAD ad = new NativeExpressAD(this, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT), appid, posId, new NativeExpressAD.NativeExpressADListener() {
            @Override
            public void onADLoaded(List<NativeExpressADView> list) {
                AdTools.upload(AdTools.Native, AdTools.show);
                hud.dismiss();
                for (NativeExpressADView adView : list) {
                    layout.addView(adView);
                    adView.render();
                }
            }

            @Override
            public void onRenderFail(NativeExpressADView nativeExpressADView) {

            }

            @Override
            public void onRenderSuccess(NativeExpressADView nativeExpressADView) {

            }

            @Override
            public void onADExposure(NativeExpressADView nativeExpressADView) {

            }

            @Override
            public void onADClicked(NativeExpressADView nativeExpressADView) {
                if(index==5){
                    Utils.toast(null,"今天恰饱了！谢谢！");
                    finish();
                    return;
                }
                AdTools.upload( AdTools.Native, AdTools.click);
               index++;

            }

            @Override
            public void onADClosed(NativeExpressADView nativeExpressADView) {

            }

            @Override
            public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
            }

            @Override
            public void onADOpenOverlay(NativeExpressADView nativeExpressADView) {

            }

            @Override
            public void onADCloseOverlay(NativeExpressADView nativeExpressADView) {
                nativeExpressADView.destroy();
            }

            @Override
            public void onNoAD(AdError adError) {
                hud.dismiss();
                Toast.makeText(NativeAdActivity.this,"现在并不想恰饭",Toast.LENGTH_LONG).show();
            }
        });
        ad.loadAD(5);
    }
}
