package com.example.leudaemialikeme.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
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 java.text.SimpleDateFormat;
import java.util.Locale;

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();
    }

    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", Locale.getDefault());
                String time = sdf.format(timeCurrent);
                Log.e("输入的time:", time);
                new Thread(new Runnable(){
                    @Override
                    public void run() {
                        try {
                            Data app = (Data)getApplication();
                            question.qInsert(app.uid,title,detail,time,0,0,0);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SendQuestionActivity.this,
                                            "提交成功",Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (Exception exception) {
                            exception.printStackTrace();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SendQuestionActivity.this,
                                            "问题提交失败,请检查您的网络状况",Toast.LENGTH_SHORT).show();
                                }
                            });

                        }
                    }
                }).start();
            }
            }
        });


    }


    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);
                    }
                }
            }
        });

    }

}



