package com.bc_chat.im.module;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.bc_chat.im.activity.MapLocationActivity;

import io.rong.imkit.RongExtension;
import io.rong.imkit.plugin.CombineLocationPlugin;
import io.rong.imkit.plugin.location.AMapRealTimeActivity;
import io.rong.imkit.plugin.location.LocationManager;
import io.rong.imkit.utilities.OptionsPopupDialog;
import io.rong.imkit.utilities.PermissionCheckUtil;

/**
 * author：Created by zhaohaoting on 2018/11/20
 * email：526309416@qq.com
 * desc：
 */
public class MCombineLocationPlugin extends CombineLocationPlugin {
    @Override
    public void onClick(Fragment currentFragment, RongExtension extension) {
        String[] permissions = new String[]{"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_NETWORK_STATE"};
        if (PermissionCheckUtil.checkPermissions(currentFragment.getActivity(), permissions)) {
            this.sendOrShareLocation(currentFragment, extension);
        } else {
            extension.requestPermissionForPluginResult(permissions, 255, this);
        }

    }

    private void sendOrShareLocation(final Fragment currentFragment, final RongExtension extension) {
        String[] items = new String[]{currentFragment.getString(io.rong.imkit.R.string.rc_plugin_location_message), currentFragment.getString(io.rong.imkit.R.string.rc_plugin_location_sharing)};
        OptionsPopupDialog.newInstance(currentFragment.getActivity(), items).setOptionsPopupDialogListener(new OptionsPopupDialog.OnOptionsItemClickedListener() {
            public void onOptionsItemClicked(int which) {
                if (which == 0) {
                    Intent intentx = new Intent(currentFragment.getActivity(), MapLocationActivity.class);
                    extension.startActivityForPluginResult(intentx, 1, MCombineLocationPlugin.this);
                } else if (which == 1) {
                    int result = LocationManager.getInstance().joinLocationSharing();
                    if (result == 0) {
                        Intent intent = new Intent(currentFragment.getActivity(), AMapRealTimeActivity.class);
                        currentFragment.getActivity().startActivity(intent);
                    } else if (result == 1) {
                        Toast.makeText(currentFragment.getActivity(), io.rong.imkit.R.string.rc_network_exception, 0).show();
                    } else if (result == 2) {
                        Toast.makeText(currentFragment.getActivity(), io.rong.imkit.R.string.rc_location_sharing_exceed_max, 0).show();
                    }
                }

            }
        }).show();
    }

    public boolean onRequestPermissionResult(Fragment fragment, RongExtension extension, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (PermissionCheckUtil.checkPermissions(fragment.getActivity(), permissions)) {
            this.sendOrShareLocation(fragment, extension);
        } else {
            extension.showRequestPermissionFailedAlter(PermissionCheckUtil.getNotGrantedPermissionMsg(fragment.getActivity(), permissions, grantResults));
        }

        return true;
    }

}
