package com.example.leudaemialikeme.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leudaemialikeme.Dao.QuestionDao;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.Data;
import com.example.leudaemialikeme.Utils.OkHttpUtil;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class SendQuestionActivity extends BaseActivity {

    EditText titleText;
    EditText detailText;
    TextView cancelView;
    TextView commitView;
    TextView textCountView;
    TextView titleCountView;
    String title;
    String detail;
    QuestionDao question=new QuestionDao();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_question);
        initViews();
        setWatcher();
        setClick();
    }

    @SuppressWarnings("unchecked")
    private void setClick() {
        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendQuestionActivity.this.finish();
            }
        });

        commitView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                title=titleText.getText().toString();
                detail=detailText.getText().toString();
                if(titleText.getText().toString().length()<10){
//                    Log.e("输入的title:", String.valueOf(title.length()));
                    Toast.makeText(SendQuestionActivity.this,
                            "您输入的字数尚不足，请完善您的问题以便获得更好的回答",Toast.LENGTH_SHORT).show();
                }
                else{
                long timeCurrent = System.currentTimeMillis();
                SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                String time = sdf.format(timeCurrent);
//                Log.e("输入的time:", time);
                Map<String,String> paramMap = new HashMap<String,String>();
                Data app = (Data)getApplication();

                paramMap.put("uid",String.valueOf(BaseActivity.owner.getNetId()));  // 需要获取登陆用户的id：需改


//                Log.e("uid",String.valueOf(app.uid));
//                for(String key:paramMap.keySet())
//                {
//                    Log.e("Key: "+key," Value: "+paramMap.get(key));
//                }
                paramMap.put("qtittle",title);
                paramMap.put("qcontent",detail);
                paramMap.put("qtime",time);
                paramMap.put("qfollowNum",String.valueOf(0));
                paramMap.put("qanswerNum",String.valueOf(0));
                paramMap.put("qbrowseNum",String.valueOf(0));
                paramMap.put("flag",String.valueOf(0));
                sendQuestionRequest(paramMap);

            }
            }
        });


    }

    private void sendQuestionRequest(final Map paramMap){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url=SendQuestionActivity.SERVER_URL+"/question-servlet?action=send-question";
                    String jsonStr= OkHttpUtil.synPost(url, paramMap);
//                    for(Object key:paramMap.keySet())
//                    {
//                        Log.e("Key: "+key," Value: "+paramMap.get(key));
//                    }
                    QuestionResponse(jsonStr);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }


    private void QuestionResponse(final String jsonStr){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (jsonStr.contains("success")){
                    Toast.makeText(SendQuestionActivity.this,
                            "提交成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(SendQuestionActivity.this,
                            "问题提交失败,请检查您的网络状况", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }



    public void initViews(){
        titleText=(EditText)findViewById(R.id.editText_question);
        detailText=(EditText)findViewById(R.id.editText_question_detail);
        cancelView=(TextView)findViewById(R.id.cancel);
        commitView=(TextView)findViewById(R.id.commit);
        textCountView=(TextView)findViewById(R.id.text_count);
        titleCountView=(TextView)findViewById(R.id.title_count);
//        title=titleText.getText().toString();
//        detail=detailText.getText().toString();
    }

    public void setWatcher(){
        titleText.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp=s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                int editStart = titleText.getSelectionStart();
                int editEnd = titleText.getSelectionEnd();
                titleCountView.setText(temp.length()+"/40");
                if(temp.length()>40){
                    Toast.makeText(SendQuestionActivity.this,
                            "您输入的字数已经超过限制",Toast.LENGTH_SHORT).show();
                    s.delete(editStart-1,editEnd);
                    int tempSelection=editStart;
                    detailText.setText(s);
                    try{
                        detailText.setSelection(tempSelection);
                    } catch (Exception exception) {
                        detailText.setText(s);
                    }
                }
            }
        });

        detailText.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp=s;
            }
            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                int editStart = detailText.getSelectionStart();
                int editEnd = detailText.getSelectionEnd();
                textCountView.setText(temp.length()+"/200");
                if(temp.length()>200){
                    Toast.makeText(SendQuestionActivity.this,
                            "您输入的字数已经超过限制",Toast.LENGTH_SHORT).show();
                    s.delete(editStart-1,editEnd);
                    int tempSelection=editStart;
                    detailText.setText(s);
                    try{
                        detailText.setSelection(tempSelection);
                    } catch (Exception exception) {
                        detailText.setText(s);
                    }
                }
            }
        });

    }

}



