package com.itant.shiwushu.ui.menu.post.publish;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itant.shiwushu.bean.LostFound;
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 PublishPostPresenter implements PublishPostContract.Presenter {

    private PublishPostContract.View view;

    public PublishPostPresenter(PublishPostContract.View view) {
        this.view = view;
    }

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

    @Override
    public void publishPost(int cityId, String cityName, String phoneNumber, int money, int postType, int lostType, String content, File photoFile) {
        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("cityId", String.valueOf(cityId))
                .addFormDataPart("cityName", cityName)
                .addFormDataPart("phoneNumber", phoneNumber)
                .addFormDataPart("money", String.valueOf(money))
                .addFormDataPart("postType", String.valueOf(postType))
                .addFormDataPart("lostType", String.valueOf(lostType))
                .addFormDataPart("content", String.valueOf(content));

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

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

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

                if (view != null) {
                    view.onPublishFailed("发布失败");
                }
            }

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