package com.sipai.jspatrol.Patrol.patrolList;

import android.content.Context;

import com.sipai.jspatrol.utils.okHttpCookie.CookiesManager;
import com.sipai.jspatrol.Base.BaseModel;
import com.sipai.jspatrol.Bean.LoginInfo;
import com.sipai.jspatrol.Bean.User;

import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * created by lvliming on 2019/9/11
 */
public class PatrolListModel extends BaseModel<PatrolListPresenter, PatrolListContract.Model> {
    public PatrolListModel(PatrolListPresenter patrolListPresenter){
        super(patrolListPresenter);
    }

    @Override
    public PatrolListContract.Model getContract() {
        return new PatrolListContract.Model() {
            @Override
            public void executeData(Context context,HashMap<String, String> map, String server, String url) throws Exception {
                CookiesManager.clearCookie(context);
                OkHttpClient.Builder clientBuilder = new OkHttpClient().newBuilder();
                clientBuilder.cookieJar(new CookiesManager());
                OkHttpClient client = clientBuilder.build();
                FormBody.Builder builder = new FormBody.Builder();
                if (map!=null){
                    for (String key:map.keySet()){
                        builder.add(key,map.get(key));
                    }
                }
                FormBody body = builder.build();
                Request request = new Request.Builder()
                        .url(server+url)
                        .post(body)
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        LoginInfo item = new LoginInfo();
                        item.setStatus(false);
                        item.setReason("网络请求失败，请重试！");
                        p.getContract().responseResult(item);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res = response.body().string();
                        LoginInfo item = new LoginInfo();
                        item = parseData(res);
                        p.getContract().responseResult(item);
                    }
                });
            }
        };
    }

    private LoginInfo parseData(String res) {
        LoginInfo item = new LoginInfo();
        try {
            JSONObject jsonObject = new JSONObject(res);
            item.setStatus(jsonObject.optBoolean("status"));
            if (jsonObject.optBoolean("status")){
                JSONObject js = new JSONObject(jsonObject.optString("res"));
                User rs = new User();
                rs.setId(js.opt("id").toString());
                rs.setCaption(js.opt("caption").toString());
                rs.setName(js.opt("name").toString());
                rs.setLastlogintime(js.opt("lastlogintime").toString());
                rs.setTotaltime(js.optDouble("totaltime"));
                item.setUser(rs);
            }else {
                item.setReason(jsonObject.optString("res"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return item;
    }
}
