package com.sjkj.appthreefloor_tsgz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.SparseIntArray;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.sjkj.appthreefloor_tsgz.BuildConfig;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.activity.viewModel.MainViewModel;
import com.sjkj.appthreefloor_tsgz.adapter.ViewPagerAdapter;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityMainBinding;
import com.sjkj.appthreefloor_tsgz.fragment.EventFragment;
import com.sjkj.appthreefloor_tsgz.fragment.MainFragment;
import com.sjkj.appthreefloor_tsgz.fragment.MineFragment;
import com.sjkj.appthreefloor_tsgz.utils.VersionUtils;
import com.sjkj.base_lib.app.AppManager;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseActivity;

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

public class MainActivity extends BaseActivity {
    ActivityMainBinding binding;
    MainViewModel viewModel;
    private MainFragment mainFragment = new MainFragment();
    private EventFragment eventFragment = new EventFragment();
    private MineFragment mineFragment = new MineFragment();
    private ViewPagerAdapter pagerAdapter;
    private SparseIntArray items;

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initBindingView(Bundle savedInstanceState) {
        super.initBindingView(savedInstanceState);
        if (savedInstanceState != null) {
            Intent intent = new Intent(this, SplashActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    }

    @Override
    public void initBinding() {
        binding = (ActivityMainBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }

    @Override
    public void initView() {
        super.initView();
        fragmentArrayList.add(eventFragment);
        fragmentArrayList.add(mainFragment);
        fragmentArrayList.add(mineFragment);
//        binding.oneBottomLayout.setFragmentManager(getSupportFragmentManager(), binding.viewPager);
//        binding.oneBottomLayout.addFragment(R.id.tab1, eventFragment);
//        binding.oneBottomLayout.addFragment(R.id.tab2, mainFragment);
//        binding.oneBottomLayout.addFragment(R.id.tab3, mineFragment);
        pagerAdapter = new ViewPagerAdapter(MainActivity.this.getSupportFragmentManager(), fragmentArrayList);
        binding.viewPager.setAdapter(pagerAdapter);
        binding.viewPager.setCurrentItem(1);

//        binding.mNavigation.enableAnimation(false);
//        binding.mNavigation.enableShiftingMode(false);
//        binding.mNavigation.enableItemShiftingMode(false);
//        binding.mNavigation.setTextVisibility(true);
        binding.oneBottomLayout.setItemColorStateList(R.drawable.selector_navigation_tab_color);
        binding.oneBottomLayout.attachViewPager(getSupportFragmentManager(), binding.viewPager, fragmentArrayList);

//        binding.mNavigation.setData(NavigationDateUtils.createDefaultData());
//        binding.mNavigation.setSelectPosition(1);
//        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                //binding.mNavigation.setCurrentItem(position);
//                //binding.mNavigation.getMenu().getItem(position).setChecked(true);
//                //binding.mNavigation.setSelectPosition(position);
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
        //设置监听
//        binding.mNavigation.setOnMyItemClickListener((view, position) -> {
//            switch (position) {
//                case 0:
//                    binding.mNavigation.setSelectPosition(0);
//                    binding.viewPager.setCurrentItem(0);
//                    break;
//                case 1:
//                    binding.mNavigation.setSelectPosition(1);
//                    binding.viewPager.setCurrentItem(1);
//                    break;
//                case 2:
//                    binding.mNavigation.setSelectPosition(2);
//                    binding.viewPager.setCurrentItem(2);
//                    break;
//
//            }
//        });
//        binding.mNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
//
//            @Override
//            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//                switch (item.getItemId()) {
//                    //通榆/四平
//                    case R.id.action_event:
//                        binding.viewPager.setCurrentItem(0);
//                        break;
//                    case R.id.action_main:
//                        binding.viewPager.setCurrentItem(1);
//                        break;
//
//                    case R.id.action_mine:
//                        binding.viewPager.setCurrentItem(2);
//                        break;
//                    default:
//                        break;
//                }
//
//                return false;
//            }
//        });


//        AppTools.initLink(this);
    }

    @Override
    public void loadData() {
//        //版本信息校验
//        VersionUtils versionUtils = new VersionUtils(AppManager.getAppManager().currentActivity());
//        versionUtils.checkVersion(BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, new VersionUtils.CallBack() {
//            @Override
//            public void onError(String msg) {
//                ToastUtils.show(msg + "");
//            }
//
//            @Override
//            public void onSuccess() {
//            }
//
//            @Override
//            public void onNewest() {
//                ToastUtils.show("当前版本为最新版本");
//            }
//        });
//        DhUtils.getInstance().initServiceInfo();
    }

    @Override
    public void initListener() {

    }

    private List<Fragment> fragmentArrayList = new ArrayList<>();

    private Boolean isExit = false;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    public void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            System.exit(0);
        }
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }

    };
}