package ezviz.ezopensdkcommon.configwifi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import ezviz.ezopensdkcommon.R;
import ezviz.ezopensdkcommon.common.IntentConstants;
import ezviz.ezopensdkcommon.common.RootActivity;
import ezviz.ezopensdkcommon.common.TitleBar;

public class AutoWifiPrepareStepOneActivity extends RootActivity implements OnClickListener {

    private Button btnNext;
    private Button btnIntroduce;
    private ImageView imageBg;
    private TextView topTip;
    private CheckBox cbLedBlink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auto_wifi_prepare_step_on);
        initTitleBar();
        initUI();
    }

    private void initTitleBar() {
        TitleBar mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle(R.string.auto_wifi_step_one_title);
        mTitleBar.addBackButton(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    private void initUI() {
        topTip = (TextView) findViewById(R.id.topTip);
        imageBg = (ImageView) findViewById(R.id.imageBg);
        btnNext = (Button) findViewById(R.id.btnNext);
        btnIntroduce = (Button) findViewById(R.id.btnIntroduce);
        cbLedBlink = (CheckBox) findViewById(R.id.cbLedBlink);

        btnNext.setOnClickListener(this);
        btnIntroduce.setOnClickListener(this);

        // 加载设备图片：优先使用上个页面传递的远程图片URL
        String devicePicUrl = getIntent().getStringExtra(IntentConstants.EXTRA_DEVICE_PIC_URL);
        if (!TextUtils.isEmpty(devicePicUrl)) {
            Glide.with(this)
                    .load(devicePicUrl)
                    .asBitmap()
                    .placeholder(R.drawable.device_other)
                    .into(imageBg);
        } else {
            imageBg.setImageResource(R.drawable.device_other);
        }

        // checkbox控制下一步按钮可用状态
        cbLedBlink.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                btnNext.setEnabled(isChecked);
            }
        });
    }

    @Override
    public void onClick(View v) {

        Intent intent = null;
        int id = v.getId();
        if (id == R.id.btnNext){
            intent = new Intent(this, AutoWifiNetConfigActivity.class);
            intent.putExtras(getIntent());
            startActivity(intent);
        }else if(id == R.id.btnIntroduce){
            intent = new Intent(this, AutoWifiResetActivity.class);
            intent.putExtras(getIntent());
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
