package com.navinfo.outdoor.activity;

import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseActivity;
import com.navinfo.outdoor.fragment.FindFragment;
import com.navinfo.outdoor.fragment.MineFragment;
import com.navinfo.outdoor.fragment.RecordFragment;
import com.navinfo.outdoor.fragment.TreasureFragment;
import com.navinfo.outdoor.util.BackHandlerHelper;
import com.navinfo.outdoor.util.NoSlideViewPager;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.navinfo.outdoor.R;
import com.google.android.material.tabs.TabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.navinfo.outdoor.util.TecentLocationUtils;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * 首页
 */
public class HomeActivity extends BaseActivity{
    private boolean isExit=false;//标识是否退出
    private NoSlideViewPager mViewPager;
    private TabLayout mTab;
    private final String[] names = {"寻宝", "记录", "发现", "我的"};
    private final int[] pic = {R.drawable.selector_map,R.drawable.selector_task,R.drawable.selector_article,R.drawable.selector_mine};
    private RecordFragment recordFragment;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            isExit=false;
            return false;
        }
    });

    @Override
    protected int getLayout() {
        return R.layout.activity_home;
    }
    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }

    }
    @Override
    protected void initView() {
        super.initView();
        ImmersionBar.with(this)
                .navigationBarColor(R.color.tipTextColor)
                .statusBarDarkFont(true, 0.2f)
                //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                .init();
        mTab=findViewById(R.id.tab_layout);
        mViewPager=findViewById(R.id.no_slide_view_pager);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new TreasureFragment());
        recordFragment = new RecordFragment();
        fragments.add(recordFragment);
        fragments.add(new FindFragment());
        fragments.add(new MineFragment());
         mViewPager.setOffscreenPageLimit(3);

        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    recordFragment.refreshData();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setCanScroll(true);
        mViewPager.setNoScrollAnim(false);
        mTab.setupWithViewPager(mViewPager);
        mTab.getTabAt(0).setText(names[0]).setIcon(pic[0]);
        mTab.getTabAt(1).setText(names[1]).setIcon(pic[1]);
        mTab.getTabAt(2).setText(names[2]).setIcon(pic[2]);
        mTab.getTabAt(3).setText(names[3]).setIcon(pic[3]);
        mTab.getTabAt(0).select();
        mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            //禁止table layout效果
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition()==1){
                    Message obtain = Message.obtain();
                    obtain.what = Constant.JOB_WORD_MONITOR;
                    obtain.obj = true;
                    EventBus.getDefault().post(obtain);
                }else if (tab.getPosition()==3){
                    Message obtain = Message.obtain();
                    obtain.what = Constant.HOME_MINE;
                    obtain.obj = true;
                    EventBus.getDefault().post(obtain);
                }
            }

            //点击table layout效果
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }
    @Subscribe
    public void onEvent(Message data) {
        if (data.what == Constant.EVENT_WORK_HOME) {
           if ((boolean)data.obj){
               mTab.getTabAt(0).select();
           }
        }
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(this, "再按一次后退键退出程序", Toast.LENGTH_SHORT).show();
            handler.sendEmptyMessageDelayed(0, 2000);  // 利用handler延迟发送更改状态信息
        } else {
            this.finish();
        }
    }
    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }

    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this))//加上判断
            EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

}