package com.example.leudaemialikeme.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.OkHttpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AnswerActivity extends AppCompatActivity {
    private TextView text_answer_cancel;
    private TextView text_answer_commit;
    private TextView text_question_name;
    private EditText edit_answer_detail;
    private int qid;
    private String qtitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        qid = intent.getIntExtra("qid", -1);
        qtitle = intent.getStringExtra("qtitle");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);

        text_answer_cancel = findViewById(R.id.text_answer_cancel);
        text_answer_commit = findViewById(R.id.text_answer_commit);
        text_question_name = findViewById(R.id.text_question_name);
        edit_answer_detail = findViewById(R.id.text_answer_detail);
        text_question_name.setText(qtitle);

        text_answer_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        text_answer_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(edit_answer_detail.getText().toString().equals("")){
                    Toast.makeText(AnswerActivity.this, "请输入回答内容", Toast.LENGTH_SHORT).show();
                }else{
                    AlertDialog.Builder builder = new AlertDialog.Builder(AnswerActivity.this);
                    builder.setTitle("是否确认提交回答？");
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(AnswerActivity.this, "已取消回答", Toast.LENGTH_SHORT).show();
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String content = edit_answer_detail.getText().toString();
                            Map<String, String> params = new HashMap<>();
                            params.put("answerContent", content);
                            sendAnswer(params);
                        }
                    });
                    builder.show();
                }

            }
        });
    }

    private void sendAnswer(Map params){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url = LoginActivity.SERVER_URL+"/main-servlet?action=sendAnswer&iduser="+ BaseActivity.owner.getNetId()+"&qid="+qid;
                    OkHttpUtil.asyPost(url, params,new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String jsonStr = response.body().string();
                            runOnUiThread(new Runnable(){
                                @Override
                                public void run() {
                                    if(jsonStr.equals("success")){
                                        Toast.makeText(getApplicationContext(), "回答成功", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }else {
                                        Toast.makeText(getApplicationContext(), "回答失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    });

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
}