package com.ym.klccy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.ym.admodule.config.AdInfo;
import com.ym.admodule.config.AdManager;
import com.ym.admodule.config.ZXADSizeConfig;
import com.ym.admodule.listener.ZXADFullVideoListener;
import com.ym.admodule.listener.ZXADVideoListener;
import com.ym.admodule.sh.CoralRewardVideo;
import com.ym.modulecommon.base.SplashResumeActivity;
import com.ym.modulecommon.utils.AdJumpUtils;
import com.ym.modulecommon.utils.LogUtils;
import com.ym.modulecommon.utils.MainLooper;
import com.ym.modulecommon.utils.PhoneUtils;
import com.ym.modulecommon.utils.Utils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout fm_contanier;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm_contanier = findViewById(R.id.fm_contanier);
        Button btn_feed_module = findViewById(R.id.btn_feed_module);
        Button btn_feed_native = findViewById(R.id.btn_feed_native);
        Button btn_fullvideo = findViewById(R.id.btn_fullvideo);
        Button btn_reward = findViewById(R.id.btn_reward);
        Button btn_show_reward = findViewById(R.id.btn_show_reward);
        Button btn_show_fullvideo = findViewById(R.id.btn_show_fullvideo);
        Button btn_load_fullvideo = findViewById(R.id.btn_load_fullvideo);
        Button btn_show_sh = findViewById(R.id.btn_show_sh);
        Button btn_show_sh_reward = findViewById(R.id.btn_show_sh_reward);
        Button btn_show_sm_reward = findViewById(R.id.btn_show_sm_reward);
        Button btn_show_sm_fullscreen = findViewById(R.id.btn_show_sm_fullscreen);
        Button btn_load_feed_ad = findViewById(R.id.btn_load_feed_ad);
        Button btn_show_feed_ad = findViewById(R.id.btn_show_feed_ad);
        Button btn_show_splash = findViewById(R.id.btn_show_splash);
        Button btn_load_reward = findViewById(R.id.btn_load_reward);
        Button btn_jump_news_home = findViewById(R.id.btn_jump_news_home);
        Button btn_jump_inter = findViewById(R.id.btn_jump_inter);
        Button btn_close_inter = findViewById(R.id.btn_close_inter);
        Button btn_show_inter = findViewById(R.id.btn_show_inter);
        btn_show_inter.setOnClickListener(this);
        btn_jump_inter.setOnClickListener(this);
        btn_close_inter.setOnClickListener(this);

        btn_show_sm_reward.setOnClickListener(this);
        btn_show_sm_fullscreen.setOnClickListener(this);
        btn_show_sh_reward.setOnClickListener(this);
        btn_show_sh.setOnClickListener(this);
        btn_feed_module.setOnClickListener(this);
        btn_feed_native.setOnClickListener(this);
        btn_fullvideo.setOnClickListener(this);
        btn_reward.setOnClickListener(this);
        btn_load_feed_ad.setOnClickListener(this);
        btn_show_feed_ad.setOnClickListener(this);
        btn_show_splash.setOnClickListener(this);
//        btn_load_reward.setOnClickListener(this);
        btn_show_reward.setOnClickListener(this);
        btn_load_reward.setOnClickListener(this);
        btn_load_fullvideo.setOnClickListener(this);
        btn_show_fullvideo.setOnClickListener(this);
        btn_jump_news_home.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_feed_module://信息流 模板
                loadFeedModule();
//                HlAdTest.loadFeedHl2(this,fm_contanier);
                break;
            case R.id.btn_feed_native://信息流 原生
                loadFeedNative();
                break;
            case R.id.btn_fullvideo://播放全屏视屏
                loadFullVideo();
                break;
            case R.id.btn_reward://激励视频
                loadReward();
//                HlAdTest.loadHlReward2(this);
                break;
//            case R.id.btn_load_reward://加载激励视频
//                load2Reward();
//                break;
            case R.id.btn_load_reward:
                AdManager.INSTANCE.loadNextCacheRewardVideoAd(ADConfig.INSTANCE.getAD_VIDEO(), this);
//                HlAdTest.loadHlReward(this);
                break;
            case R.id.btn_show_reward://展示激励视频
//                HlAdTest.showHlReward();
                showCacheReward();
            case R.id.btn_load_fullvideo://预加载全屏
                AdManager.INSTANCE.loadNextCacheFullVideoAd(ADConfig.INSTANCE.getAD_FULL_VIDEO(), this);
                break;
            case R.id.btn_show_fullvideo://展示全屏视频
                showCacheFull();
                break;
            case R.id.btn_show_sh://展示珊瑚内容联盟
                showSHContent();
                break;
            case R.id.btn_show_sh_reward://展示珊瑚激励视频
                showSHReward();
                break;
            case R.id.btn_show_sm_reward://展示SignMob激励视频
                showSMReward();
                break;
            case R.id.btn_show_sm_fullscreen://展示SignMob全屏视频
                showSMFullScreen();
                break;
            case R.id.btn_load_feed_ad://与加载大图广告
                AdManager.INSTANCE.loadFeedAdToCache(ADConfig.INSTANCE.getAD_SETTING_BIGIMAGE(), this,
                        new ZXADSizeConfig(Utils.px2dip(PhoneUtils.INSTANCE.getScreenWidth(this)) - 20, 300));
//                HlAdTest.loadFeedHl(this);

                break;
            case R.id.btn_show_feed_ad://展示大图广告
                AdManager.INSTANCE.showFeedAd(ADConfig.INSTANCE.getAD_SETTING_BIGIMAGE(), "main_load_big_img",
                        new ZXADSizeConfig(Utils.px2dip(PhoneUtils.INSTANCE.getScreenWidth(this)) - 20, 300), this, fm_contanier);

//                HlAdTest.showFeeHl(this,fm_contanier);
                break;
            case R.id.btn_show_splash:
                Intent intent = new Intent(this, SplashResumeActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_jump_news_home:
                AdJumpUtils.INSTANCE.jumpAdNewsHomeActivity(this);

//                AdManager.INSTANCE.showInterstitialAd(this, "Interstitial", "xxxssss", new ZXADSizeConfig(PhoneUtils.INSTANCE.getScreenWidth(this) - Utils.dip2px(100), Utils.dip2px(500)), new ZXADFullVideoListener() {
//                    @Override
//                    public void onAdClose(boolean isSkippedVideo) {
//
//                    }
//
//                    @Override
//                    public void onSuccess() {
//
//                    }
//
//                    @Override
//                    public void onError(@Nullable String errorMsg) {
//
//                    }
//
//                    @Override
//                    public void onAdShow() {
//
//                    }
//
//                    @Override
//                    public void onAdVideoBarClick() {
//
//                    }
//
//                    @Override
//                    public void onAdClose() {
//
//                    }
//                });
                break;
            case R.id.btn_jump_inter:
                int width = Utils.px2dip(PhoneUtils.INSTANCE.getScreenWidth(this) - Utils.dip2px(60));
                int heigt = (width*3)/2;
                LogUtils.e("hloadInterstitialAd    开始事件" +System.currentTimeMillis());
                AdManager.INSTANCE.loadInterstitialAd(this,"Interstitial",new ZXADSizeConfig(width,heigt));
//                new ZXADSizeConfig(PhoneUtils.INSTANCE.getScreenWidth(this)-Utils.dip2px(100), Utils.dip2px(500)

                break;

            case R.id.btn_show_inter:
                int width1 = Utils.px2dip(PhoneUtils.INSTANCE.getScreenWidth(this) - Utils.dip2px(60));
                int heigt1 = (width1*3)/2;
                AdManager.INSTANCE.showInterstitialAd(this, "Interstitial", "xx", new ZXADSizeConfig(width1, heigt1), new ZXADFullVideoListener() {
                    @Override
                    public void onAdClose(boolean isSkippedVideo, @NotNull AdInfo adInfo) {

                    }

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError(@Nullable String errorMsg) {

                    }

                    @Override
                    public void onAdShow() {

                    }

                    @Override
                    public void onAdVideoBarClick() {

                    }

                    @Override
                    public void onAdClose(@NotNull AdInfo adInfo) {

                    }
                });

                break;
            case R.id.btn_close_inter:
                MainLooper.Companion.get().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        AdManager.INSTANCE.destroyInteractionAd();
                    }
                },3000);

                break;
            default:
                break;
        }
    }


    private void showCacheFull() {
        AdManager.INSTANCE.playFullVideoAd(ADConfig.INSTANCE.getAD_VIDEO(), "我是测试", this,
                new ZXADFullVideoListener() {
                    @Override
                    public void onAdClose(boolean isSkippedVideo, AdInfo adInfo) {
                        LogUtils.d("MainActivity dddddddd", "视频播放成功"+adInfo.getCodeId());
                    }

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError(@Nullable String errorMsg) {
                        LogUtils.d("huang", "视频播放出错" + errorMsg);
                    }

                    @Override
                    public void onAdShow() {
//                        AdManager.INSTANCE.loadNextCacheRewardVideoAd(ADConfig.INSTANCE.getAD_VIDEO(), MainActivity.this);
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        LogUtils.d("MainActivity", "onAdVideoBarClick");
                    }

                    @Override
                    public void onAdClose(AdInfo adInfo) {
                        LogUtils.d("MainActivity", "视频播放成功"+adInfo.getCodeId());
//                        AdManager.INSTANCE.loadNextCacheRewardVideoAd(ADConfig.INSTANCE.getAD_VIDEO(), MainActivity.this);
                    }
                });
    }

    private void showCacheReward() {
        AdManager.INSTANCE.playRewardAd(ADConfig.INSTANCE.getAD_VIDEO(), "我是测试", this, new ZXADVideoListener() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(@Nullable String errorMsg) {
                LogUtils.d("huang", "视频播放出错" + errorMsg);


            }

            @Override
            public void onAdShow() {
//                        AdManager.INSTANCE.loadNextCacheRewardVideoAd(ADConfig.INSTANCE.getAD_VIDEO(), MainActivity.this);
            }

            @Override
            public void onAdVideoBarClick() {
                LogUtils.d("MainActivity", "onAdVideoBarClick");
            }

            @Override
            public void onAdClose(AdInfo adInfo) {
                LogUtils.d("MainActivity", "视频播放成功   "+ adInfo.getCodeId());
//                        AdManager.INSTANCE.loadNextCacheRewardVideoAd(ADConfig.INSTANCE.getAD_VIDEO(), MainActivity.this);
            }
        });
    }

    private void showSMFullScreen() {
//        SMADUtils.INSTANCE.loadScreenVideoAd(this, null, null, null);
    }

    private void showSMReward() {
//        SMADUtils.INSTANCE.loadRewardVideoAd(this, null, null, null);
    }

    private void showSHReward() {
//        SHADUtils.INSTANCE.loadRewardVideoAd(this,null,null,null);
        new CoralRewardVideo(this, 104).pull();
    }

    private void showSHContent() {
        AdManager.INSTANCE.loadSHFeedSpecialVideoAd(this, R.id.fm_container_view);
    }

    private Object rewardVideoAD1;


    //加载 激励视频
    private void loadReward() {
        AdManager.INSTANCE.loadRewardVideoAd(ADConfig.INSTANCE.getAD_VIDEO(), "", this, new ZXADVideoListener() {
            @Override
            public void onAdVideoBarClick() {

            }

            @Override
            public void onAdClose(AdInfo adInfo) {

            }

            @Override
            public void onAdShow() {

            }

            @Override
            public void onError(@Nullable String errorMsg) {

            }

            @Override
            public void onSuccess() {

            }
        });

    }

    //全屏
    private void loadFullVideo() {
        AdManager.INSTANCE.loadFullVideoAd(ADConfig.INSTANCE.getAD_FULL_VIDEO(),
                this, "", new ZXADFullVideoListener() {

                    @Override
                    public void onAdClose(boolean isSkippedVideo,AdInfo adInfo) {
                        Utils.showToast(MainActivity.this,"isSkippedVideo:"+isSkippedVideo);
                    }

                    @Override
                    public void onAdClose(AdInfo adInfo) {

                    }

                    @Override
                    public void onAdVideoBarClick() {

                    }

                    @Override
                    public void onAdShow() {

                    }

                    @Override
                    public void onError(@Nullable String errorMsg) {

                    }

                    @Override
                    public void onSuccess() {

                    }
                });
    }

    private void loadFeedNative() {

    }


    //模板
    private void loadFeedModule() {
        AdManager.INSTANCE.loadFeedAd(ADConfig.INSTANCE.getAD_SETTING_BIGIMAGE(), "action", this,
                new ZXADSizeConfig(Utils.px2dip(PhoneUtils.INSTANCE.getScreenWidth(this)) - 20, 300),
                fm_contanier);

    }

    @Override
    protected void onPause() {
        super.onPause();
//        AdManager.INSTANCE.loadFeedAdToCache(ADConfig.INSTANCE.getAD_SETTING_BIGIMAGE(), this,
//                new ZXADSizeConfig(Utils.px2dip(PhoneUtils.INSTANCE.getScreenWidth(this)) - 20, 300));
    }
}
