package com.tjzhxx.union;

import android.Manifest;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.tjzhxx.union.permission.PermissionHelper;
import com.tjzhxx.union.permission.PermissionInterface;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.LogUtils;
import com.tjzhxx.union.public_store.SpUtils;
import com.tjzhxx.union.public_store.fragment.FragmentTab;
import com.tjzhxx.union.public_store.fragment.FragmentVisitor;
import com.tjzhxx.union.system.BaseActivity;

import butterknife.BindView;

/**
 * 描述:
 *
 * @author yanghuaxiong
 * @create 2021/9/1 11:43 AM
 */
public class MainActivity extends BaseActivity implements FragmentVisitor.FragmentVisitorListener, PermissionInterface {

    @BindView(R.id.fragment_continer)
    FrameLayout fragmentContiner;
    @BindView(R.id.mt_home)
    FragmentTab mtHome;
    @BindView(R.id.mt_union)
    FragmentTab mtUnion;
    @BindView(R.id.mt_group)
    FragmentTab mtGroup;
    @BindView(R.id.mt_personal)
    FragmentTab mtPersonal;
    public FragmentVisitor fragmentVisitor;
    public int currIndex = 0;
    private FragmentTab[] majorTabs;
    private Class<? extends Fragment>[] fragments;
    private long exit_time;
    private PermissionHelper mPermissionHelper;

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

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        init();
    }

    private void init() {
        mPermissionHelper = new PermissionHelper(this, this);
        ConstDefine.userInfo = SpUtils.getUserInfo(this);
        fragmentContiner.removeAllViews();
        fragmentVisitor = new FragmentVisitor();
        majorTabs = new FragmentTab[]{mtHome, mtUnion, mtGroup, mtPersonal};
        fragments = new Class[]{HomeFragment.class, UnionFragment.class, GroupFragment.class, MyFragment.class};
        fragmentVisitor.init(getSupportFragmentManager(), this)
                .setFragmentContiner(R.id.fragment_continer)
                .setTabAndFragmentData(majorTabs, fragments);
        fragmentVisitor.setCurrentItem(0);
        mPermissionHelper.requestPermissions();
    }

    @Override
    public void onItemChanged(int oldItem, int newItem, String tabDiscription) {
        currIndex = newItem;
    }

    @Override
    public boolean boforeItemChangedItemEnable(int currentItem, int newItem) {

        return true;
    }

    @Override
    public void controlItemEnable(boolean enable) {

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {// 是后退键
            long temp = System.currentTimeMillis();
            if (temp - exit_time <= 3000) {
                finish(); // 主界面退出
            } else {
                exit_time = temp;
                showSnackBar("再按一次退出程序");
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void startLocaion() {
        AMapLocationClient mLocationClient = new AMapLocationClient(this);
        mLocationClient.setLocationListener(mLocationListener);
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);

        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }

    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //定位成功回调信息，设置相关消息
                    ConstDefine.city = amapLocation.getCity();
                    ConstDefine.district = amapLocation.getDistrict();
                    LogUtils.e("TAG", ConstDefine.city + ConstDefine.district);
                } else {
                    //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                    LogUtils.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }
    };

    @Override
    public int getPermissionsRequestCode() {
        return 999;
    }

    @Override
    public String[] getPermissions() {
        return new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_FINE_LOCATION
        };
    }

    @Override
    public void requestPermissionsSuccess() {
        startLocaion();
    }

    @Override
    public void requestPermissionsFail() {
        showSnackBar("权限被拒绝");
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[]
                                                   grantResults) {
        if (mPermissionHelper.requestPermissionsResult(requestCode, permissions, grantResults)) {
            //权限请求结果，并已经处理了该回调
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
