package com.yutou.jianr_mg.views;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;

import com.classichu.lineseditview.LinesEditView;
import com.umeng.analytics.MobclickAgent;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Data.SystemData;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.Network.HttpApi;
import com.yutou.jianr_mg.Network.HttpUtils;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.ActivitysManager;
import com.yutou.jianr_mg.Tools.Utils;

import org.json.JSONObject;

import me.drakeet.materialdialog.MaterialDialog;

public class UpFeedBack extends AppCompatActivity {
    private LinesEditView editView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();
    }

    private void initView() {
        editView = findViewById(R.id.linesEditView);
        TextView info = findViewById(R.id.info);
        Button button = findViewById(R.id.button);
        Switch aSwitch = findViewById(R.id.switch1);
        info.setText(new SystemData().toString());
        aSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isChecked) {
                MaterialDialog dialog = new MaterialDialog(UpFeedBack.this);
                dialog.setTitle("提示");
                dialog.setMessage("关闭采集可能让我们无法查明原因哦~\n采集内容并不包含隐私问题请放心");
                dialog.setNegativeButton("允许采集", view -> {aSwitch.setChecked(true);dialog.dismiss();});
                dialog.setPositiveButton("我拒绝", view -> {aSwitch.setChecked(false);dialog.dismiss();});
                dialog.show();
            }
        });
        button.setOnClickListener(v -> {
            String context = editView.getContentText();
            if (context.trim().length() == 0) {
                Utils.toast(UpFeedBack.this, "请不要提交空内容");
                return;
            }
            editView.setEnabled(false);
            try {
                JSONObject json = new JSONObject();
                json.put("context", editView.getContentText().trim());
                if (aSwitch.isChecked())
                    json.put("sysinfo", new SystemData().toString());
                else {
                    json.put("sysinfo","User say:NO!");
                }
                HttpUtils.post(HttpApi.HOME_URL + HttpApi.SYSTEM_UP_INFO, json, new HttpInterface() {
                    @Override
                    public void httpGetData(String string, int code) {
                        String msg = "error";
                        try {
                            JSONObject json = new JSONObject(string);
                            msg = json.getString("msg");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String finalMsg = msg;
                        AppData.handler.post(() -> {
                            Utils.toast(UpFeedBack.this, finalMsg);
                            if (finalMsg.equals("提交成功")) {
                                ActivitysManager.finishActivity(UpFeedBack.class);
                            } else {
                                editView.setEnabled(true);
                            }
                        });
                    }

                    @Override
                    public void httpError(Exception e) {
                        AppData.handler.post(() -> Utils.toast(UpFeedBack.this, "网络错误"));
                        editView.setEnabled(true);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
