package com.fxy.main;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import com.jaeger.library.StatusBarUtil;
import com.jpeng.jptabbar.BadgeDismissListener;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
import com.fxy.baselibrary.base.BaseActivity;
import com.fxy.baselibrary.base.BaseFragment;
import com.fxy.baselibrary.util.AppManager;
import com.fxy.R;
import com.fxy.base.StorageApplication;
import com.fxy.main.adapter.MainViewPagerAdapter;
import com.fxy.main.fragment.HomePagerFragment;
import com.fxy.main.fragment.MineFragment;
import com.fxy.utils.AllDialogShowStrategy;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import org.lzh.framework.updatepluginlib.UpdateBuilder;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import me.weyye.hipermission.HiPermission;
import me.weyye.hipermission.PermissionCallback;
import me.weyye.hipermission.PermissionItem;


/**
 * 主面板界面
 * @author James.Jiang
 * @date 2020-01-05
 */
public class MainActivity extends BaseActivity implements BadgeDismissListener, OnTabSelectListener {
    /**
     *定义主面板底部标题
     */
    @Titles
    private static final int[] mTitles = { R.string.tab1, R.string.tab3};
    /**
     *定义主面板底部样式
     */
    @SeleIcons
    private static final int[] mSeleIcons = {R.drawable.ic_home_blue, R.drawable.ic_mine_blue};
    /**
     *定义主面板底部图标
     */
    @NorIcons
    private static final int[] mNormalIcons = {R.drawable.ic_home_gray,R.drawable.ic_mine_gray};

    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @BindView(R.id.tabbar)
    JPTabBar mTabbar;

    HomePagerFragment homePagerFragment;

    MineFragment mineFragment;

    MainViewPagerAdapter mainViewPagerAdapter;

    List<BaseFragment> mDatas;

    // 首页tab页签数
    public static final int ITEMPAGER = 2;

    @Override
    public int getContentViewResId() {
        return R.layout.activity_main;
    }

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

    @Override
    public boolean isPlayMusic() {
        return true;
    }

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

    @Override
    protected void getBundleExtras(Bundle extras) {
    }



    @Override
    protected void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.md_deep_orange_300), 0);
        HiPermission();
        ButterKnife.bind(this);
        mTabbar.setSelectedColor(ContextCompat.getColor(this, R.color.md_orange_STO));
        mTabbar.setPageAnimateEnable(false);
        initFragment();
        StorageApplication.context.isShowUpdateToast = false;
        UpdateBuilder.create().setUpdateStrategy(new AllDialogShowStrategy()).check();
    }

    private void initFragment() {
        mDatas = new ArrayList<>();
        homePagerFragment = HomePagerFragment.newInstance(null);
        mineFragment = new MineFragment();
        mDatas.add(homePagerFragment);
        mDatas.add(mineFragment);

        mainViewPagerAdapter = new MainViewPagerAdapter(getSupportFragmentManager(), mDatas);
        mViewPager.setAdapter(mainViewPagerAdapter);
        mTabbar.setContainer(mViewPager);
//        mainViewPagerAdapter.setmFragments(mDatas);
    }

    @Override
    public void onDismiss(int position) {
    }


    @Override
    public void onTabSelect(int index) {
    }

    @Override
    public boolean onInterruptSelect(int index) {
//        if(index==2){
//            //如果这里有需要阻止Tab被选中的话,可以return true
//            return true;
//        }
        return false;
    }

    public JPTabBar getTabbar() {
        return mTabbar;
    }

    //记录用户首次点击返回键的时间
    private long firstTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                showToast(getString(R.string.exit_double));
                firstTime = System.currentTimeMillis();
            } else {
                AppManager.getAppManager().AppExit(this, false);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void HiPermission(){
        AndPermission.with(this)
                .permission(
                        Permission.Group.STORAGE,
                        Permission.Group.CAMERA,
                        Permission.Group.LOCATION,
                        Permission.Group.MICROPHONE
                );
        List<PermissionItem> permissionItems = new ArrayList<PermissionItem>();
        permissionItems.add(new PermissionItem(Manifest.permission.READ_PHONE_STATE, "读写", R.drawable.permission_ic_storage));
        permissionItems.add(new PermissionItem(Manifest.permission.RECORD_AUDIO, "录音", R.drawable.permission_ic_storage));
        permissionItems.add(new PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE, "读写", R.drawable.permission_ic_micro_phone));
        permissionItems.add(new PermissionItem(Manifest.permission.CAMERA, "相机", R.drawable.permission_ic_camera));
        HiPermission.create(MainActivity.this)
                .title("温馨提示")
                .permissions(permissionItems)
                .msg("请务必授予该权限!")
                .checkMutiPermission(new PermissionCallback() {
                    @Override
                    public void onClose() {
						showToast("用户关闭权限申请，部分功能会影响");
//                        showWarningToast(getString(com.sto.fingerstorage.R.string.warning_permission));
                    }

                    @Override
                    public void onFinish() {
//						showToast("所有权限申请完成");
                    }

                    @Override
                    public void onDeny(String permission, int position) {
//						Log.i(TAG, "onDeny");
                    }

                    @Override
                    public void onGuarantee(String permission, int position) {
//						Log.i(TAG, "onGuarantee");
                    }
                });
    }

}


