package com.android.chmo.ui.activity.me;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.LoginRes;
import com.android.chmo.http.response.PhotoRes;
import com.android.chmo.http.service.UserService;
import com.android.chmo.im.IMManager;
import com.android.chmo.model.LoginUser;
import com.android.chmo.ui.activity.LoginActivity;
import com.android.chmo.ui.dialog.ConfirmDialog;
import com.android.chmo.ui.dialog.SelectDialog;
import com.android.chmo.utils.CommonUtils;
import com.android.chmo.utils.FileInfoUtils;
import com.android.chmo.utils.LoginUtils;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;
import com.netease.nim.uikit.common.media.picker.activity.PickImageActivity;
import com.netease.nim.uikit.support.permission.MPermission;
import com.netease.nim.uikit.support.permission.annotation.OnMPermissionDenied;
import com.netease.nim.uikit.support.permission.annotation.OnMPermissionGranted;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/20.
 */

public class SettingsActivity extends BaseActivity {
    private final int RESULT_MODNAME = 1;
    private final int RESULT_CAMERA = 2;
    private final int RESULT_PHOTO = 3;

    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;

    @Override
    protected int getContentResId() {
        return R.layout.activity_settings;
    }

    @Override
    public void initView() {
        LoginUser user = LoginUtils.getLoginUser();
        fillView(user);
    }

    private void fillView(LoginUser user) {
        tvName.setText(user.getPet());
        tvPhone.setText(user.getPhone());
        XUtilsImage.displayCircluar(ivAvatar, HttpApi.getImgUrl(user.getHphoto()), R.mipmap.def_head);
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    private void getUserInfo() {
        UserService.getUser(new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                LoginRes res = new Gson().fromJson(result, LoginRes.class);
                if (res.data != null) {
                    LoginUser user = new LoginUser();
                    user.setUser(res.data);
                    LoginUtils.saveLoginUser(user);
                    IMManager.updateUser();
                    initView();
                }
            }

            @Override
            public void onFailure(String error) {
            }
        });
    }

    @OnClick({R.id.ll_avatar, R.id.ll_name, R.id.btn_logout})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_avatar:
                String[] strs = new String[]{"拍照", "选择相册照片"};
                new SelectDialog(getContext())
                        .setItems(strs)
                        .setItemListener(new SelectDialog.ItemListener() {
                            @Override
                            public void onSelect(int pos) {
                                if (pos == 0) {
                                    pickFromCamera();
                                } else {
                                    pickFromPhoto();
                                }

                            }
                        }).show();
                break;
            case R.id.ll_name:
                Intent intent = new Intent(getContext(), ModifyNameActivity.class);
                openPageForResult(intent, RESULT_MODNAME);
                break;
            case R.id.btn_logout:
                new ConfirmDialog(this)
                        .setMsg("是否确定退出？")
                        .setDialogListener(new ConfirmDialog.DialogListener() {
                            @Override
                            public void onOk(Dialog dialog) {
                                dialog.dismiss();
                                LoginUtils.clearLoginUser();
                                CommonUtils.clearAllNotification(getContext());
                                IMManager.logout();
                                openPage(LoginActivity.class);
                                close();
                            }

                            @Override
                            public void onCancel(Dialog dialog) {
                                dialog.dismiss();
                            }
                        }).show();
                break;
        }
    }

    private void pickFromCamera() {
        startActivityForResult(PickImageActivity.getStartIntent(this, PickImageActivity.FROM_CAMERA, getOutputPath(), false, 1,
                false, true, 150, 150), RESULT_CAMERA);

    }

    private void pickFromPhoto() {
        startActivityForResult(PickImageActivity.getStartIntent(this, PickImageActivity.FROM_LOCAL, getOutputPath(), false, 1,
                false, true, 150, 150), RESULT_PHOTO);
    }

    private String getOutputPath() {
        String path = FileInfoUtils.getPhotoPath(getContext());
        return path;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case RESULT_MODNAME:
                getUserInfo();
                break;
            case RESULT_CAMERA:
            case RESULT_PHOTO:
                String path = data.getStringExtra("file_path");
                updateHead(path);
                break;
        }
    }

    private void updateHead(String path) {
        showLoading("上传头像...");
        UserService.updateHead(path, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                PhotoRes res = new Gson().fromJson(result, PhotoRes.class);
                if (res.msg.equals("success")) {
                    XUtilsImage.displayCircluar(ivAvatar, HttpApi.getImgUrl(res.img), R.mipmap.def_head);
                    getUserInfo();
                } else {
                    showToast("上传失败");
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("上传失败");
            }
        });
    }
}
