package ai.pai.lensman.login;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.views.progressbar.ProgressWheel;

import ai.pai.lensman.R;
import ai.pai.lensman.activities.WebViewActivity;
import ai.pai.lensman.main.MainActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends FragmentActivity implements LoginContract.View{

    @BindView(R.id.wheel_wait_http)    ProgressWheel progressWheel;
    @BindView(R.id.tv_agree_protocol)  TextView protocolText;

    private LoginContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        SpannableString content = new SpannableString(getString(R.string.agree_protocol));
        content.setSpan(new UnderlineSpan(), 6, content.length(), 0);
        protocolText.setText(content);
        presenter = new LoginPresenter(this,this);
    }


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

    @OnClick(R.id.btn_login)
    public void login(){
        presenter.login();
    }

    @OnClick(R.id.tv_agree_protocol)
    public void jump2Protocol(){
        Intent declareIntent = new Intent(this,WebViewActivity.class);
        declareIntent.putExtra("url","http://pai.ai/page/user_agreement");
        declareIntent.putExtra("title",getString(R.string.contract));
        startActivity(declareIntent);
    }

    @Override
    public void showLoginHint(String hint) {
        if(TextUtils.isEmpty(hint)){
            return;
        }
        Toast.makeText(this,hint,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgressView() {
        progressWheel.setVisibility(View.VISIBLE);
        progressWheel.startSpinning();
    }

    @Override
    public void hideProgressView() {
        progressWheel.stopSpinning();
        progressWheel.setVisibility(View.GONE);
    }

    @Override
    public void jumpMain() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

}
