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.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leudaemialikeme.Dao.InvitationDao;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.Data;

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

public class SendInvitationActivity extends BaseActivity {

    EditText titleView;
    EditText detailView;
    TextView cancelView;
    TextView commitView;
    TextView titleCountView;
    String title;
    String detail;
    String type;
    InvitationDao invitation=new InvitationDao();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_invitation);

        initView();
        setWatcher();
        setClick();
    }

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

        commitView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                title=titleView.getText().toString();
                detail=detailView.getText().toString();
                if(title.length()<10){
                    Log.e("输入的title:", String.valueOf(title.length()));
                    Toast.makeText(SendInvitationActivity.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();
                            Log.e("type",type);
                            invitation.iInsert(app.uid,type,title,detail,time,0,0);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SendInvitationActivity.this,
                                            "提交成功",Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (Exception exception) {
                            exception.printStackTrace();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SendInvitationActivity.this,
                                            "帖子提交失败,请检查您的网络状况",Toast.LENGTH_SHORT).show();
                                }
                            });

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


    }


    @SuppressLint("NonConstantResourceId")
    public void onRadioButtonClicked(View view) {
        RadioButton button = (RadioButton) view;
        boolean isChecked = button.isChecked();
        switch (view.getId()) {
            case R.id.SendInvitation_radio_bad:
            case R.id.SendInvitation_radio_recover:
            case R.id.SendInvitation_radio_experience:
            case R.id.SendInvitation_radio_knowledge:
                if (isChecked) {
                    type=button.getText().toString();
                }
                break;
            default:
                type="经验帖";
                break;
        }
    }
    public void initView(){
        titleView=(EditText)findViewById(R.id.invitation_title);
        detailView=(EditText)findViewById(R.id.invitation_detail);
        cancelView=(TextView)findViewById(R.id.invitation_cancel);
        commitView=(TextView)findViewById(R.id.invitation_commit);
        titleCountView=(TextView)findViewById(R.id.invitation_title_count);
    }

    public void setWatcher(){
        titleView.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 = titleView.getSelectionStart();
                int editEnd = titleView.getSelectionEnd();
                titleCountView.setText(temp.length()+"/40");
                if(temp.length()>40){
                    Toast.makeText(SendInvitationActivity.this,
                            "您输入的字数已经超过限制",Toast.LENGTH_SHORT).show();
                    s.delete(editStart-1,editEnd);
                    int tempSelection=editStart;
                    titleView.setText(s);
                    try{
                        titleView.setSelection(tempSelection);
                    } catch (Exception exception) {
                        titleView.setText(s);
                    }
                }
            }
        });
    }

}
