package com.android.chmo.ui.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseFragment;
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.activity.me.AboutActivity;
import com.android.chmo.ui.activity.me.ModifyNameActivity;
import com.android.chmo.ui.activity.me.MySignActivity;
import com.android.chmo.ui.activity.me.RechargeActivity;
import com.android.chmo.ui.activity.order.MyOrderActivity;
import com.android.chmo.ui.activity.order.VideoOrderActivity;
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 butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/31.
 */

public class MeFragment extends BaseFragment {
    private final int RESULT_MODNAME = 1;

    private final int RESULT_CAMERA = 2;

    private final int RESULT_PHOTO = 3;

    @BindView(R.id.head)
    ImageView headView;

    @BindView(R.id.nickName)
    TextView nickNameView;

    @BindView(R.id.phone)
    TextView phoneView;

    @BindView(R.id.balance)
    TextView balanceView;



    @Override
    public int getLayoutRes() {
        return R.layout.page_me;
    }

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


    }

    private void fillView(LoginUser user) {
        nickNameView.setText(user.getPet());
        phoneView.setText(user.getPhone());
        balanceView.setText(user.getCoin_a() + "币");
        XUtilsImage.displayCircluar(headView, HttpApi.getImgUrl(user.getHphoto()),R.mipmap.def_head);
    }

    @Override
    public void onVisible() {
        getUserInfo();
    }

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

    @OnClick(R.id.myHead)
    public void clickHead() {
        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();
    }

    @OnClick(R.id.myNickName)
    public void clickNickName() {
        Intent intent = new Intent(getContext(), ModifyNameActivity.class);
        openPageForResult(intent, RESULT_MODNAME);
    }

    @OnClick(R.id.myAccount)
    public void clickAccount() {
        openPage(RechargeActivity.class);
    }

    @OnClick(R.id.myOrder)
    public void clickOrder() {
        openPage(MyOrderActivity.class);
    }

    @OnClick(R.id.videoOrder)
    public void videoOrder() {
        openPage(VideoOrderActivity.class);
    }

    @OnClick(R.id.mySign)
    public void clickSign() {
        openPage(MySignActivity.class);
    }

    @OnClick(R.id.about)
    public void about() {
        openPage(AboutActivity.class);
    }

    @OnClick(R.id.logout)
    public void clickLogout() {
        new ConfirmDialog(getBaseActivity())
                .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();

    }


    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();
                    fillView(user);
                }
            }

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


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

    }

    private void pickFromPhoto() {
        startActivityForResult(PickImageActivity.getStartIntent(getActivity(), 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.display(headView,HttpApi.getImgUrl(res.img),R.mipmap.def_head);
                    getUserInfo();
                }else {
                    showToast("上传失败");
                }
            }

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