package com.yutou.jianr_mg;

import android.os.Bundle;
import android.os.Handler;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.yutou.jianr_mg.Adapters.ViewPagerAdapter;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Fragments.GameData;
import com.yutou.jianr_mg.Fragments.Home;
import com.yutou.jianr_mg.Fragments.MGList;
import com.yutou.jianr_mg.Fragments.My;
import com.yutou.jianr_mg.Fragments.UtilsFragments;
import com.yutou.jianr_mg.Tools.ActivitysManager;
import com.yutou.jianr_mg.Tools.JianRUtils;

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

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MainActivity extends AppCompatActivity {

    private PageNavigationView tab;
    private ViewPager viewPager;
    private NavigationController  navigationController;
    private   List<Fragment> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppData.activity=this;
        AppData.handler=new Handler();
        ActivitysManager.finishActivity(LoadingActivity.class);
        initViews();
        JianRUtils.downlaodInit(true);
        JianRUtils.readClientVersionIsbigzhao(this);
        new Thread(()->{
            JianRUtils.reloadJianNiang();
            JianRUtils.reloadEquipmnts();
            JianRUtils.reloadShipType();
        }).start();
    }

    private void initViews() {
        tab = findViewById(R.id.tab);
        viewPager = findViewById(R.id.pager);
        list=new ArrayList<>();
        list.add(Home.init());
        list.add(UtilsFragments.init());
        list.add(GameData.init());
        list.add(MGList.init());
        list.add(My.init());

        ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        navigationController = tab.material()
                .addItem(R.drawable.icon_home, R.drawable.icon_home_select, "主页")
                .addItem(R.drawable.icon_tools,R.drawable.icon_tools_select,"工具")
                .addItem(R.drawable.icon_data, R.drawable.icon_data_select, "数据")
                .addItem(R.drawable.icon_mg, R.drawable.icon_mg_select, "魔改")
                .addItem(R.drawable.icon_my, R.drawable.icon_my_select, "个人中心")
                .build();
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                switch (index) {
                }
                viewPager.setCurrentItem(index);
            }

                @Override
                public void onRepeat(int index) {

            }
        });
        navigationController.setupWithViewPager(viewPager);
        navigationController.setSelect(0);
    }
    long oldTime=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(System.currentTimeMillis()-oldTime<1000){
                ActivitysManager.AppExit(this);
            }else{
                Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_LONG).show();
                oldTime=System.currentTimeMillis();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
