package com.sobot.demo.activity.more;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sobot.demo.SobotSPUtil;
import com.sobot.demo.R;

/**
 * Created by Administrator on 2017/11/21.
 */

public class SobotTransferCustomActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText sobot_isArtificialIntelligence_num;//如果是只能转人工，需要设置当应到问题出现几次以后显示转人工按钮
    private EditText sobot_demo_transferKeyWord;//转人工关键字
    private RelativeLayout sobot_isArtificialIntelligence;//是否智能转人工,如果是，需要设置setArtificialIntelligenceNum为大于等于1的数字，默认是1
    private RelativeLayout sobot_isQueueFirst;//是否排队优先
    private RelativeLayout sobot_tv_left;
    private boolean isUseVoice = false;//是否显示咨询信息，默认不显示
    private boolean isQueueFirst = false;//是否排队优先
    private ImageView imgOpenVoice;
    private ImageView img_isQueueFirst;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sobot_transfer_custom_activity);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        findvViews();
        isUseVoice =SobotSPUtil.getBooleanData(this, "sobot_isArtificialIntelligence", isUseVoice);
        setVoice(isUseVoice);
        isQueueFirst=SobotSPUtil.getBooleanData(this, "sobot_isQueueFirst", isQueueFirst);
        setQueueFirst(isQueueFirst);

    }

    private void findvViews(){
        sobot_tv_left = (RelativeLayout) findViewById(R.id.sobot_demo_tv_left);
        sobot_tv_left .setOnClickListener(this);
        TextView sobot_text_title = (TextView) findViewById(R.id.sobot_demo_tv_title);
        sobot_text_title.setText("转人工设置");
        sobot_isArtificialIntelligence_num = (EditText) findViewById(R.id.sobot_isArtificialIntelligence_num);
        sobot_demo_transferKeyWord = (EditText) findViewById(R.id.sobot_demo_transferKeyWord);
        sobot_isArtificialIntelligence = (RelativeLayout) findViewById(R.id.sobot_isArtificialIntelligence);
        sobot_isQueueFirst = (RelativeLayout) findViewById(R.id.sobot_isQueueFirst);
        imgOpenVoice = (ImageView) findViewById(R.id.img_open_notify);
        img_isQueueFirst = (ImageView) findViewById(R.id.img_isQueueFirst);
        sobot_isArtificialIntelligence.setOnClickListener(this);
        sobot_isQueueFirst.setOnClickListener(this);

        getSobotStartSet();
    }

    private void getSobotStartSet() {
        String sobot_isArtificialIntelligence_num_value = SobotSPUtil.getStringData(this, "sobot_isArtificialIntelligence_num_value", "");
        if (!TextUtils.isEmpty(sobot_isArtificialIntelligence_num_value)) {
            sobot_isArtificialIntelligence_num.setText(sobot_isArtificialIntelligence_num_value);
        }

        boolean sobot_bool_isArtificialIntelligence = SobotSPUtil.getBooleanData(this, "sobot_isArtificialIntelligence", false);
        setVoice(sobot_bool_isArtificialIntelligence);

        String sobot_demo_transferKeyWord_value = SobotSPUtil.getStringData(this, "sobot_transferKeyWord", "");
        if (!TextUtils.isEmpty(sobot_demo_transferKeyWord_value)) {
            sobot_demo_transferKeyWord.setText(sobot_demo_transferKeyWord_value);
        }
    }

    private void saveSobotStartSet() {
        SobotSPUtil.saveBooleanData(this, "sobot_isArtificialIntelligence", isUseVoice);
        SobotSPUtil.saveStringData(this,"sobot_isArtificialIntelligence_num_value",sobot_isArtificialIntelligence_num.getText().toString());
        SobotSPUtil.saveStringData(this, "sobot_transferKeyWord", sobot_demo_transferKeyWord.getText().toString());
    }

    @Override
    public void onBackPressed() {
        saveSobotStartSet();
        finish();
    }

    @Override
    public void onClick(View v) {
        if (v == sobot_tv_left){
            saveSobotStartSet();
            finish();
        } else if (v == sobot_isArtificialIntelligence){
            isUseVoice = !isUseVoice;
            setVoice(isUseVoice);
        } else if (v == sobot_isQueueFirst){
            isQueueFirst = !isQueueFirst;
            setQueueFirst(isQueueFirst);
        }
    }

    private void setVoice(boolean open){
        if(open){
            isUseVoice = true;
            imgOpenVoice.setBackgroundResource(R.drawable.sobot_demo_icon_open);
            SobotSPUtil.saveBooleanData(this, "sobot_isArtificialIntelligence", isUseVoice);
        }else{
            isUseVoice = false;
            imgOpenVoice.setBackgroundResource(R.drawable.sobot_demo_icon_close);
            SobotSPUtil.saveBooleanData(this, "sobot_isArtificialIntelligence", isUseVoice);
        }
    }

    private void setQueueFirst(boolean open){
        if(open){
            isQueueFirst = true;
            img_isQueueFirst.setBackgroundResource(R.drawable.sobot_demo_icon_open);
            SobotSPUtil.saveBooleanData(this, "sobot_isQueueFirst", isQueueFirst);
        }else{
            isQueueFirst = false;
            img_isQueueFirst.setBackgroundResource(R.drawable.sobot_demo_icon_close);
            SobotSPUtil.saveBooleanData(this, "sobot_isQueueFirst", isQueueFirst);
        }
    }
}