package ai.pai.client.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.common.executors.ThreadExecutor;
import ai.pai.client.utils.HttpPostTask;
import com.android.common.utils.NetworkUtil;

import java.util.HashMap;

import ai.pai.client.R;
import ai.pai.client.db.Preferences;
import ai.pai.client.utils.SystemUtils;
import ai.pai.client.utils.UrlContainer;

public class FeedbackActivity extends BaseActivity implements View.OnClickListener{

    private Button   sendBtn;
    private EditText feedbackET;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        SystemUtils.setImmerseLayout(this,findViewById(R.id.title_layout));
        TextView title = (TextView)findViewById(R.id.title_bar_middle_txt);
        title.setText(R.string.slide_feedback);
        feedbackET = (EditText)findViewById(R.id.et_feedback_content);
        feedbackET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sendBtn.setEnabled(s.toString().length()>0);
            }
        });
        sendBtn = (Button) findViewById(R.id.btn_feedback_send);
        sendBtn.setOnClickListener(this);
        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
        findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_bar_back_layout:
                finish();
                break;
            case R.id.btn_feedback_send:
                sendFeedback();
                break;
            default:
                break;
        }
    }

    private void sendFeedback() {
        if(!sendBtn.isEnabled()){
            return;
        }
        if(!NetworkUtil.isNetworkConnected(this)){
            Toast.makeText(this,R.string.network_disconnect,Toast.LENGTH_SHORT).show();
            return;
        }
        String feedback = feedbackET.getText().toString();
        if(TextUtils.isEmpty(feedback)){
            return;
        }
        HashMap<String,String> params = new HashMap<String,String>();
        String userId = Preferences.getInstance(this).getUserId();
        if(userId.length()==0){
            return;
        }
        params.put("user_id",userId);
        params.put("feedback",feedback);
        new HttpPostTask(this,params).executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), UrlContainer.FEEDBACK_URL);
        Toast.makeText(this,R.string.thank_feedback,Toast.LENGTH_SHORT).show();
        finish();
    }
}
