package com.itant.shiwushu.ui.menu.me.info;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itant.shiwushu.bean.ResponseResult;
import com.itant.shiwushu.bean.User;
import com.itant.shiwushu.constant.ResultCode;
import com.itant.shiwushu.constant.ServerHost;
import com.itant.shiwushu.manager.UserManager;
import com.itant.shiwushu.net.ElasticCallback;
import com.itant.shiwushu.tool.RequestClient;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Jason on 2018/10/8.
 */

public class UserInfoPresenter implements UserInfoContract.Presenter {

    private UserInfoContract.View view;

    public UserInfoPresenter(UserInfoContract.View view) {
        this.view = view;
    }

    public void setView(UserInfoContract.View view) {
        this.view = view;
    }

    @Override
    public void updateUserData(String nickname, String saying, File headFile, String suffix) {
        User user = UserManager.getInstance().getUser();

        OkHttpClient client = RequestClient.getClient().getNormalOkHttpClient();
        MultipartBody.Builder builder =new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("userId", user.getUserId())//添加键值对参数
                .addFormDataPart("token", user.getToken())
                .addFormDataPart("nickname", nickname)
                .addFormDataPart("saying", saying);

        if (headFile != null) {
            // 文件不为空才添加文件，否则会闪退
            builder.addFormDataPart("file", headFile.getName(), RequestBody.create(MediaType.parse("file/*"), headFile))
                    .addFormDataPart("suffix", suffix);
        }
        MultipartBody multipartBody = builder.build();
        final Request request = new Request.Builder()
                .url(ServerHost.URL_UPDATE_USER_DATA)
                .post(multipartBody)
                .build();
        client.newCall(request).enqueue(new ElasticCallback() {

            @Override
            public void onRequestFailure(Call call, IOException e) {
                e.printStackTrace();
                if (view != null) {
                    view.onUpdateFailed("更新失败");
                }
            }

            @Override
            public void onRequestResponse(int code, String result) throws IOException {
                if (code == 200) {
                    ResponseResult<User> responseResult = JSON.parseObject(result, new TypeReference<ResponseResult<User>>(){});
                    if (responseResult != null && view != null) {
                        if (responseResult.getResultCode() == ResultCode.SUCCESS) {
                            User user = responseResult.getResultObj();
                            view.onUpdateSuccess(user);
                            return;
                        } else {
                            view.onUpdateFailed(responseResult.getMessage());
                            return;
                        }
                    }
                }

                if (view != null) {
                    view.onUpdateFailed("更新失败");
                }
            }

            @Override
            public void onRequestComplete() {
                if (view != null) {
                    view.onRequestComplete();
                }
            }
        });
    }
}
