package com.ycgis.macall.personalcenter.v.activity.travel;

import android.content.Intent;
import android.os.Bundle;

import com.google.gson.JsonObject;
import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.utils.LogUtils;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.rs.macall.androidx.basemodel.utils.TypConversion;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ActivityTravelDetailsBinding;
import com.ycgis.macall.personalcenter.p.app.RuanseeApplication;
import com.ycgis.macall.personalcenter.p.callback.BaseRequestCallback;
import com.ycgis.macall.personalcenter.p.request.ApiModel;
import com.ycgis.macall.personalcenter.p.request.CommonCompositeManage;
import com.ycgis.macall.personalcenter.p.request.RetrofitService;

import org.json.JSONException;
import org.json.JSONObject;

public class TravelDetailsActivity extends BaseViewBindActivity<ActivityTravelDetailsBinding> {
    private String cityName;
    // 1 是 司局级人员     2 是一般人员
    private String level;

    @Override
    protected ActivityTravelDetailsBinding getViewBinding() {
        return ActivityTravelDetailsBinding.inflate(getLayoutInflater());
    }

    @Override
    protected boolean isImmersiveStatusBar() {
        return true;
    }

    @Override
    protected void init() {

    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            Intent intent = getIntent();
            cityName = intent.getStringExtra("cityName");
            level = intent.getStringExtra("level");
        }
    }

    @Override
    protected void start() {
        if ("1".equals(level)){
            viewBinding.travelDetalisTitle.contentTitle.setText("司局级报销标准");
        }else {
            viewBinding.travelDetalisTitle.contentTitle.setText("一般人员报销标准");
        }
        viewBinding.travelDetalisTitle.leftImage.setImageDrawable(getDrawable(R.drawable.back_cliener));
        viewBinding.travelDetalisTitle.titleleft.setOnClickListener(v -> finish());
        baseShowDialog("正在加载请稍后...");
        viewBinding.travelTvCityName.setText(String.format("%s差旅报销标准", cityName));
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CommonCompositeManage.getInstance().unDisposable("getCitystandard");
    }

    private void getData() {
        ApiModel.request("getCitystandard",
                RetrofitService.getBaseInstance().getMoneyCity(RuanseeApplication.getUserData().getUserCode(), cityName, level)
                , new BaseRequestCallback<JsonObject>() {
                    @Override
                    public void onRequestFailure(String msg) {
                        baseDismissDialog();
                        LogUtils.e(TAG, msg);
                        ToastUtil.centered(getContext(),msg);
                    }

                    @Override
                    public void onRequestSuccess(JsonObject result) {
                        baseDismissDialog();
                        LogUtils.w(TAG, result.toString());
                        //{"code":20041,"data":{"carfare":"火车：软席(软座、软卧),高铁/动车一等座,全列软席列车一等软座;轮船：二等舱;飞机：经济舱","content":"600元/人天","taxi":"80元/人天","wages":"100元/人天"},"msg":""}
                        try {
                            JSONObject object = new JSONObject(result.toString());
                            int code = TypConversion.getJsonInt(object, "code", -1);
                            if (code == 20041) {
                                JSONObject data = TypConversion.getJSONObject(object, "data");
                                String carfare = TypConversion.getJsonStr(data, "carfare");
                                String[] split = carfare.split(";");
                                for (int i = 0; i < split.length; i++) {
                                    String[] split1 = split[i].split("：");
                                    if (i == 0) {
                                        String[] split2 = split1[1].split(",");
                                        String sp = "";
                                        StringBuilder builder = new StringBuilder();
                                        for (int j = 0; j < split2.length; j++) {
                                            builder.append(sp);
                                            builder.append(split2[j]);
                                            sp="\n";
                                        }
                                        viewBinding.travelTvTrain.setText(StringUtil.get(builder.toString(),"暂无"));
                                        continue;
                                    }
                                    if (i == 1){
                                        viewBinding.travelTvShips.setText(StringUtil.get(split1[1],"暂无"));
                                    }
                                    if (i == 2){
                                        viewBinding.travelTvAircraft.setText(StringUtil.get(split1[1],"暂无"));
                                    }
                                }
                                String content1 = TypConversion.getJsonStr(data, "content");
                                String content = "暂无";
                                if (content1.contains(";")){
                                    String[] split1 = content1.split(";");
                                    String sp = "";
                                    StringBuilder builder = new StringBuilder();
                                    for (int j = 0; j <split1.length; j++) {
                                        builder.append(sp);
                                        builder.append(split1[j]);
                                        sp="\n";
                                    }
                                    content = builder.toString();
                                }else {
                                    content = content1;
                                }
                                viewBinding.travelTvStay.setText(content);
                                viewBinding.travelTvCarRepair.setText(TypConversion.getJsonStr(data, "taxi", "暂无"));
                                viewBinding.travelTvMeal.setText(TypConversion.getJsonStr(data, "wages", "暂无"));
                            }else {
                                ToastUtil.centered(getContext(),TypConversion.getJsonStr(object,"msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastUtil.centered(getContext(),e.getMessage());
                        }
                    }
                });
    }


}