package ai.pai.lensman.upgrade;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.common.utils.LogHelper;
import com.android.views.loadingdrawable.LoadingView;

import ai.pai.lensman.R;
import ai.pai.lensman.base.BaseActivity;
import ai.pai.lensman.db.Preferences;
import ai.pai.lensman.main.QueryBoxStatusInteractor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chengzhenyu on 2017/4/22.
 */

public class BoxUpgradeActivity extends BaseActivity implements BoxUpgradeInteractor.BoxUpgradeListener,QueryBoxStatusInteractor.BoxStatusListener{

    @BindView(R.id.layout_choose_update) View chooseUpdateView;
    @BindView(R.id.layout_box_updating) View  boxUpdatingView;
    @BindView(R.id.tv_updating_status) TextView updatingStatusTextView;
    @BindView(R.id.btn_return_work) Button backBtn;
    @BindView(R.id.loading_gear_view) LoadingView loadingView;
    private BoxUpgradeInteractor upgradeInteractor;
    private QueryBoxStatusInteractor boxStatusInteractor;
    private String bpversion_str;
    private int updatingVersionCode;
    private boolean isDestroyed;

    public static void startBoxUpgradeActivity(Context context,String bpversion_str,int updatingVersionCode){
        Intent intent = new Intent(context, BoxUpgradeActivity.class);
        intent.putExtra("bpversion_str",bpversion_str);
        intent.putExtra("versionCode",updatingVersionCode);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_box_upgrade);
        unbinder = ButterKnife.bind(this);
        bpversion_str = getIntent().getStringExtra("bpversion_str");
        updatingVersionCode = getIntent().getIntExtra("versionCode",0);
        if(TextUtils.isEmpty(bpversion_str)||updatingVersionCode==0){
            finish();
        }
    }

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

    @OnClick(R.id.btn_update_now)
    public void updateNow(){
        chooseUpdateView.setVisibility(View.GONE);
        boxUpdatingView.setVisibility(View.VISIBLE);
        upgradeInteractor = new BoxUpgradeInteractor(bpversion_str,this);
        upgradeInteractor.startJob();
    }

    @OnClick(R.id.btn_update_later)
    public void UpdateLater(){
        finish();
    }

    @OnClick(R.id.btn_return_work)
    public void back2work(){
        if(boxStatusInteractor!=null){
            boxStatusInteractor.cancelJob();
            boxStatusInteractor = null;
        }
        if(upgradeInteractor!=null){
            upgradeInteractor.cancelJob();
            upgradeInteractor = null;
        }
        finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBoxUpgrade(boolean result) {
        if(isDestroyed){
            return;
        }
        LogHelper.d("czy","box开始升级，开始轮询box状态");
        if(result){
            boxStatusInteractor = new QueryBoxStatusInteractor(this);
            boxStatusInteractor.startJob();
        }else{
            boxUpdatingView.setVisibility(View.GONE);
            chooseUpdateView.setVisibility(View.VISIBLE);
            Toast.makeText(this,R.string.box_updating_fail,Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBoxStatusFetched(String boxNo, String boxStatus, int boxStatusCode) {
        if(isDestroyed){
            return;
        }
        Preferences.getInstance().setBoxVersionCode(updatingVersionCode);
        updatingStatusTextView.setText(R.string.box_update_success);
        loadingView.setVisibility(View.GONE);
        backBtn.setVisibility(View.VISIBLE);
    }

}
