package com.ym.klccy;


import android.content.Intent;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.ym.admodule.config.AdManager;
import com.ym.admodule.listener.ZXADSplashListener;
import com.ym.modulecommon.base.BaseActivity;
import com.ym.modulecommon.utils.LogUtils;
import com.zxhl.detector.news.SplashContract;

public class SplashActivity extends BaseActivity implements SplashContract.View {


    private SplashPresenter mPresenter;
    private TextView tv_skipview;
    private FrameLayout splash_container;

    @Override
    public int layoutID() {
        return R.layout.splash_layout;
    }

    @Override
    public void init() {
        tv_skipview = findViewById(R.id.tv_skipview);
        splash_container = findViewById(R.id.splash_container);

        AdManager.INSTANCE.loadSplashAd(ADConfig.INSTANCE.getAD_SPLASH());
        mPresenter = new SplashPresenter(this, this);
        mPresenter.checkPermissions();//检测权限

    }

    private boolean isstartMain = false;
    private static final String TAG = "AdManager";
    @Override
    public void jumpPage() {
        //TODO  加载开屏广告
       LogUtils.d("AdManager", "广告展现  开始" + System.currentTimeMillis());

        AdManager.INSTANCE.showSplashAd(this, ADConfig.INSTANCE.getAD_SPLASH(),"splash", tv_skipview, splash_container, new ZXADSplashListener() {

            @Override
            public void onAdSkip() {
               LogUtils.d(TAG, "onAdSkip: SplashActivity");
                if (!isstartMain) {
                    isstartMain = true;
                    startMain();
                }
            }

            @Override
            public void onAdShow() {

            }

            @Override
            public void onError(@org.jetbrains.annotations.Nullable String errorMsg) {
               LogUtils.d(TAG, "SplashActivity onError: " +errorMsg);
                if (!isstartMain) {
                    isstartMain = true;
                    startMain();
                }
            }
        });
    }

    private long startTime = 0L;

    //防止两次点击
    private void startMain() {
        //用户点击了 跳过按钮   .......
        if (System.currentTimeMillis() - startTime > 2000) {
            startTime = System.currentTimeMillis();
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void jumpPermissionsPage() {
        Intent intent = new Intent(this, PermissionsActivity.class);
        intent.putExtra(PermissionsActivity.EXTRA_PERMISSIONS, SplashPresenter.Companion.getPERMISSIONS());
        ActivityCompat.startActivityForResult(this, intent, CommentConfig.REQUEST_CODE_ASK_PERMISSIONS, null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommentConfig.REQUEST_CODE_ASK_PERMISSIONS && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            //TODO 有权限的时候
           LogUtils.d("AdManager", "onActivityResult()");

            startMain();
        } else {
            mPresenter.getAppConfig();
        }
    }


    @Override
    public boolean getDestroyed() {
        return false;
    }

}
