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 com.example.leudaemialikeme.Utils.OkHttpUtil;

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

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 HH:mm:ss", Locale.getDefault());
                String time = sdf.format(timeCurrent);
                Log.e("输入的time:", time);
                Data app = (Data)getApplication();
                Map<String,String> paramMap = new HashMap<String,String>();
                paramMap.put("uid",String.valueOf(BaseActivity.owner.getNetId()));//需要获取登录用户的id
                paramMap.put("btype",type);
                paramMap.put("btittle",title);
                paramMap.put("bcontent",detail);
                paramMap.put("btime",time);
                paramMap.put("blikeNum",String.valueOf(0));
                paramMap.put("bcollectNum",String.valueOf(0));
                paramMap.put("bbrowse",String.valueOf(0));
                paramMap.put("flag",String.valueOf(0));
                sendInvitationRequest(paramMap);
                }
            }
        });
    }


    private void sendInvitationRequest(final Map paramMap){
        new Thread(new Runnable(){

            @Override
            public void run() {
                try{
                    String url=SendQuestionActivity.SERVER_URL+"/blog-servlet?action=send-invitation&useUnicode=true&characterEncoding=utf-8";
                    String jsonStr= OkHttpUtil.synPost(url, paramMap);
                    sendInvitationResponse(jsonStr);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private void sendInvitationResponse(final String jsonStr){
        runOnUiThread(new Runnable(){

            @Override
            public void run() {
                if (jsonStr.contains("success")){
                    Toast.makeText(SendInvitationActivity.this,
                            "提交成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(SendInvitationActivity.this,
                            "帖子提交失败,请检查您的网络状况",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }



    @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().substring(0,2);
                }
                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);
                    }
                }
            }
        });
    }

}
