package com.navinfo.outdoor.base;

import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.location.Location;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.navinfo.outdoor.R;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.room.ChargingPileEntity;
import com.navinfo.outdoor.room.PoiEntity;
import com.navinfo.outdoor.util.FragmentBackHandler;
import com.navinfo.outdoor.util.GPSUtils;
import com.navinfo.outdoor.util.MapManager;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;

import org.greenrobot.eventbus.EventBus;

/**
 * 管理寻宝页面的抽屉中的fragment
 */
public abstract class BaseDrawerFragment extends BaseFragment {
    public boolean isSliding = true; // fragment被销毁时，true-抽屉收回，false-抽屉状态不变
    protected PoiEntity showPoiEntity;
    public SlidingUpPanelLayout slidingPaneLayout;
    public TencentMap tencentMap;

    @Override
    protected void initData() {
        tencentMap = MapManager.getInstance().getTencentMap();
        super.initData();
        int mockGps = Constant.currentLocation.isMockGps();
        //1为是，0为否，-1为无法判断
        if (mockGps == 1) {
            onBackPressed();
        }
//        else {
//            new GPSUtils(getActivity());
//        }

    }
    @Override
    public void initEvent() {
        super.initEvent();
        isSliding = true;
        Message obtain = Message.obtain();
        obtain.what = Constant.MAIN_BUTTON_VISIABLE;
        obtain.obj = View.GONE;
        EventBus.getDefault().post(obtain);

    }
    public void moveLatlng(LatLng latLng, TencentMap.CancelableCallback cancelableCallback) {
        CameraUpdate cameraSigma = CameraUpdateFactory.newCameraPosition(new CameraPosition(
                latLng, //中心点坐标，地图目标经纬度
                tencentMap.getCameraPosition().zoom,  //目标缩放级别
                tencentMap.getCameraPosition().tilt, //目标倾斜角[0.0 ~ 45.0] (垂直地图时为0)
                tencentMap.getCameraPosition().bearing)); //目标旋转角 0~360° (正北方为0)
        tencentMap.animateCamera(cameraSigma, cancelableCallback);
    }

    @Override
    public boolean onBackPressed() {
        getActivity().getSupportFragmentManager().popBackStack();
        return true;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (showPoiEntity!=null){
            Message obtain = Message.obtain();
            obtain.what = Constant.POI_DRAWER;
            LatLng latLng = new LatLng();
            latLng.setLatitude(Double.valueOf(showPoiEntity.getY()));
            latLng.setLongitude(Double.valueOf(showPoiEntity.getX()));
            obtain.obj = latLng;
            EventBus.getDefault().post(obtain);
            MapManager.getInstance().getTencentMap().setOnMarkerClickListener(null);
        }
    }
    public void setSlidingUpPanelLayout(SlidingUpPanelLayout slidingUpPanelLayout) {
        if (slidingUpPanelLayout!=null) {
            this.slidingPaneLayout = slidingUpPanelLayout;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        MapManager.getInstance().getTencentMap().setOnMarkerClickListener(Constant.markerClickListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        showPoiEntity=null;
        Bundle arguments = getArguments();
        if (arguments != null) {
            isSliding = arguments.getBoolean("isSliding", true);
            Log.d("TAG", "onCreatessss: " + isSliding);
        }
        if (isSliding) {
            // 监听到返回按钮点击事件
            Message obtain = Message.obtain();
            obtain.what = Constant.TREASURE_FRAGMENT;
            obtain.obj = isSliding;
            EventBus.getDefault().post(obtain);
        }
        initRemovePoiSharePre();
    }

    public void initRemovePoiSharePre() {
        //获取SharedPreferences对象，方法中两个参数的意思为：第一个name
        //表示文件名，系统将会在/dada/dada/包名/shared_prefs目录下生成
        //一个以该参数命名的.xml文件。第二个mode表示创建的模式，通过查看
        //方法注释得知，建议以0或者MODE_PRIVATE为默认值。
        SharedPreferences poi = getActivity().getSharedPreferences(Constant.DATA_FILE, 0);
        //获取Editor对象
        SharedPreferences.Editor edit = poi.edit();
        //根据要保存的数据的类型，调用对应的put方法,
        //以键值对的形式添加新值。
        edit.putString("poiEntity", null);
        edit.putString("chargingPileEntity", null);
        //提交新值。必须执行，否则前面的操作都无效。
        edit.commit();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


    protected abstract PoiEntity initPoiEntityByUI( PoiEntity entity);
    protected abstract PoiCheckResult checkPoiEntity(PoiEntity entity);
    protected abstract ChargingPileEntity initChargingPileEntityByUI( ChargingPileEntity entity);
    protected abstract PoiCheckResult checkChargingPileEntity(ChargingPileEntity entity);
    public class PoiCheckResult {
        private int code; // 0 检查成功,1：失败
        private String msg;

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
