package com.cooleshow.teacher.ui.splash;

import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.common.ConstantKey;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.SPUtils;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.ViewPagerAdapter;
import com.cooleshow.teacher.databinding.ActivityGuideBinding;
import com.cooleshow.teacher.presenter.splash.GuidePresenter;
import com.cooleshow.base.widgets.MyViewPage;
import com.cooleshow.usercenter.helper.UserHelper;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/5/24 17:53
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.SplashCenter.SPLASH_GUIDE)
public class GuideActivity extends BaseMVPActivity<ActivityGuideBinding, GuidePresenter> {
    private List<View> views;
    private MyViewPage vp;
    private ViewPagerAdapter vpAdapter;
    private TextView tv_go;

    @Override
    protected void initView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        views = new ArrayList<View>();
        views.add(inflater.inflate(R.layout.guide_one, null));
        views.add(inflater.inflate(R.layout.guide_two, null));
        views.add(inflater.inflate(R.layout.guide_thre, null));
        vpAdapter = new ViewPagerAdapter(views, this);
        vp = findViewById(R.id.viewPager);
        vp.setAdapter(vpAdapter);
        tv_go = views.get(2).findViewById(R.id.tv_go);
        tv_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goAction();
                finish();
            }
        });
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarDarkTheme(this, true);
    }

    private void goAction(){
        SPUtils.getInstance().put(ConstantKey.IS_FIRST_LAUNCH, true);
        if (UserHelper.isLogin()) {
            jumpMain();
        } else {
            jumpLogin();
        }
    }
    private void jumpLogin() {
        ARouter.getInstance().build(RouterPath.UserCenter.PATH_VERIFY_LOGIN).navigation();
    }
    private void jumpMain() {
        ARouter.getInstance().build(RouterPath.APPCenter.PATH_HOME).navigation();
    }

    @Override
    protected ActivityGuideBinding getLayoutView() {
        return ActivityGuideBinding.inflate(getLayoutInflater());
    }

    @Override
    protected GuidePresenter createPresenter() {
        return new GuidePresenter();
    }


    private int currentItem = 0;

    /**
     * 滑动进入主界面
     */
    private void addListener() {
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentItem = position;
                Log.i("Guide", "监听改变" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp.setOnTouchListener(new View.OnTouchListener() {
            float startX;
            float startY;
            float endX;
            float endY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        endX = event.getX();
                        endY = event.getY();
                        WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
                        //获取屏幕的宽度
                        Point size = new Point();
                        windowManager.getDefaultDisplay().getSize(size);
                        int width = size.x;
                        //首先要确定的是，是否到了最后一页，然后判断是否向左滑动，并且滑动距离是否符合，我这里的判断距离是屏幕宽度的4分之一（这里可以适当控制）
                        if (currentItem == (views.size() - 1) && startX - endX > 0 && startX - endX >= (width / 4)) {
                            Log.e("kasdkfhsjss", "=======页面跳转=====");
                            jumpMain();
                            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_in_left);
                        }
                        break;
                }
                return false;
            }
        });
    }
}
