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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.callback.CustomClickListener;
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.ActivityOpinionsSuggestionsBinding;
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.RetrofitService;
import com.ycgis.macall.personalcenter.util.ParamMap;

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

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 意见反馈
 */
public class OpinionsSuggestionsActivity extends BaseViewBindActivity<ActivityOpinionsSuggestionsBinding> {
    private Drawable selectBJ;
    private String appId;
    private int feedType = 1;

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

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

    @Override
    protected void init() {
        selectBJ = getDrawables(R.drawable.mancolor_round_button);
        customClickListener = new CustomClickListener(500) {
            @Override
            public void onSingleClick(View v) {
                bindViewClick(v);
            }
        };
        setViewOnClickListener(viewBinding.tvGrjy);
        setViewOnClickListener(viewBinding.tvRjwt);
        setViewOnClickListener(viewBinding.tvYjwt);
        setViewOnClickListener(viewBinding.tvQt);
        setViewOnClickListener(viewBinding.btnSelectApp);
        setViewOnClickListener(viewBinding.baseTitle.titleleft);
        setViewOnClickListener(viewBinding.btnSubmit);
    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

    @Override
    protected void start() {
        viewBinding.baseTitle.leftImage.setImageDrawable(getDrawables(R.drawable.back_cliener));
        viewBinding.baseTitle.contentTitle.setText("意见反馈");
        viewBinding.baseTitle.rightText.setText("我的反馈");
        setViewOnClickListener(viewBinding.baseTitle.titleright);
        invisibleView(viewBinding.vFkyy);
    }

    @Override
    protected void bindViewClick(View v) {
        switch (v.getId()) {
            case R.id.titleright:
                startActivity(new Intent(getContext(),MySuggestionsListActivity.class));
                break;
            case R.id.titleleft:
                finish();
                break;
            case R.id.tv_grjy:
                resetBackground(viewBinding.tvGrjy);
                feedType = 1;
                invisibleView(viewBinding.vFkyy);
                break;
            case R.id.tv_rjwt:
                resetBackground(viewBinding.tvRjwt);
                visibleView(viewBinding.vFkyy);
                feedType = 2;
                break;
            case R.id.tv_yjwt:
                resetBackground(viewBinding.tvYjwt);
                invisibleView(viewBinding.vFkyy);
                feedType = 3;
                break;
            case R.id.tv_qt:
                resetBackground(viewBinding.tvQt);
                invisibleView(viewBinding.vFkyy);
                feedType = 4;
                break;
            case R.id.btn_select_app:
                startActivityForResult(new Intent(getContext(), SelectAppActivity.class), 15104);
                break;
            case R.id.btn_submit:
                save();
                break;
        }
    }

    private void save() {
        if (feedType == 2 && StringUtil.isNullOrEmpty(appId)) {
            ToastUtil.centered(getContext(), "请先选择应用");
            return;
        }
        String editText = getEditText(viewBinding.editTitle);
        if (StringUtil.isNullOrEmpty(editText)) {
            ToastUtil.centered(getContext(), "请输入反馈标题!");
            return;
        }
        String editText1 = getEditText(viewBinding.editContent);
        if (StringUtil.isNullOrEmpty(editText1)) {
            ToastUtil.centered(getContext(), "请输入反馈内容!");
            return;
        }
        ParamMap paramMap = new ParamMap();
        paramMap.put("applicationId", appId);
        paramMap.put("alarmSignal", RuanseeApplication.getUserData().getUserCode());
        paramMap.put("alarmName", RuanseeApplication.getUserData().getUserName());
        paramMap.put("type", feedType+"");
        paramMap.put("title", editText);
        paramMap.put("content", editText1);
        paramMap.put("applicationName", getTextString(viewBinding.tvSelectApp));
        String replace = new Gson().toJson(paramMap.apply());
        MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");
        RequestBody stringBody = RequestBody.Companion.create(replace, mediaType);
        baseShowDialog("正在提交，请稍后...");
        ApiModel.request(RetrofitService.getBaseInstance().addFeedBack(stringBody), 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();
                // {"code":20011,"data":null,"msg":"提交成功！"}
                LogUtils.w(TAG,result.toString());
                ApiModel.uploadOperationLog("3","3","用户提交了一条意见建议");
                try {
                    JSONObject o = new JSONObject(result.toString());
                    ToastUtil.centered(getContext(),TypConversion.getJsonStr(o,"msg"));
                    if (TypConversion.getJsonInt(o,"code",-1) == 20011){
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.centered(getContext(),e.getMessage());
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        if (requestCode == 15104 && resultCode == Activity.RESULT_OK) {
            if (data == null) {
                ToastUtil.centered(getContext(), "选择应用错误！");
                return;
            }
            appId = data.getStringExtra("appId");
            String appName = data.getStringExtra("appName");
            viewBinding.tvSelectApp.setText(appName);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void resetBackground(TextView selectView) {
        viewBinding.tvGrjy.setBackground(getDrawables(R.drawable.card_white_round_4_bk));
        viewBinding.tvGrjy.setTextColor(getColors(R.color.colorGrey6));
        viewBinding.tvRjwt.setBackground(getDrawables(R.drawable.card_white_round_4_bk));
        viewBinding.tvRjwt.setTextColor(getColors(R.color.colorGrey6));
        viewBinding.tvYjwt.setBackground(getDrawables(R.drawable.card_white_round_4_bk));
        viewBinding.tvYjwt.setTextColor(getColors(R.color.colorGrey6));
        viewBinding.tvQt.setBackground(getDrawables(R.drawable.card_white_round_4_bk));
        viewBinding.tvQt.setTextColor(getColors(R.color.colorGrey6));
        selectView.setBackground(selectBJ);
        selectView.setTextColor(getColors(R.color.colorWhite));
    }

}