package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.util.CertificationUtil;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.view.customview.TextProgressBar;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * 认证登录界面
 * Created by ljy on 2018/9/11.
 */

public class GameCertificationActivity extends BaseActivity {

    @BindView(R.id.icon_iv)
    ImageView icon_iv;
    @BindView(R.id.app_name_tv)
    TextView app_name_tv;
    @BindView(R.id.name_tv)
    TextView name_tv;
    @BindView(R.id.invite_num_tv)
    TextView invite_num_tv;
    @BindView(R.id.user_icon_iv)
    ImageView user_icon_iv;
    @BindView(R.id.change_account_tv)
    TextView change_account_tv;
    @BindView(R.id.empty_view)
    View empty_view;

    private Activity activity;
    private Bundle extras;
    private  UserEntity userEntity;
    private String packgeName;
    private TextProgressBar textProgressBar;
    private boolean isChangeAccount;

    @Override
    protected int getLayoutId() {
        return R.layout.act_game_certification;
    }

    @Override
    public void initView() {
        activity = this;
        textProgressBar = new TextProgressBar(activity);
        textProgressBar.setVisibility(View.VISIBLE);
        TitleBarUtils.getInstance().setTitle(activity, "授权登录")
                .setTitleListen(activity, 0, null);

    }

    public void initData() {
        extras = getIntent().getExtras();
        userEntity = DataUtil.getInstance().getUserEntity();

        if (extras != null) {
            SheepApp.getInstance().setGameCode(extras.getString(getString(R.string.kfzs_duanduan_datashare_game_code), ""));
            packgeName = extras.getString(getString(R.string.kfzs_duanduan_datashare_package_name), "");
            SheepApp.getInstance().setGamePackgeName(packgeName);
            if(TextUtils.isEmpty(SheepApp.getInstance().getGameCode()) || TextUtils.isEmpty(packgeName)){
                CertificationUtil.newInstance().showCertificationErrorAndExitApp("",2000);
                return;
            }
            hasToken();
        }else {
            CertificationUtil.newInstance().showCertificationErrorAndExitApp("",2000);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.icon_iv, R.id.change_account_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.icon_iv:
                textProgressBar.setVisibility(View.GONE);
                break;
            case R.id.change_account_tv:
                isChangeAccount = true;
                Jump2View.getInstance().goLoginView(activity,SheepApp.getInstance().getGameCode());
                break;
        }
    }
    /**
     * 是否登录
     * 请求授权
     */
    private void hasToken(){
        String token = SpUtils.getToken(SheepApp.getInstance());
        if(TextUtils.isEmpty(token)){
            Jump2View.getInstance().goLoginView(activity,SheepApp.getInstance().getGameCode());
        }else {
            if(userEntity == null){
                CommonUtil.getInstance().updateUserInfo(new Action1<UserEntity>() {
                    @Override
                    public void call(UserEntity userEntity) {
                        if(userEntity == null){
                            Jump2View.getInstance().goLoginView(activity,SheepApp.getInstance().getGameCode());
                        }else {
                            initWidget();
                        }
                    }
                });
            }else {
                initWidget();
            }

        }
    }
    /**
     * 赋值
     */
    private void initWidget(){
        empty_view.setVisibility(View.GONE);
        name_tv.setText(userEntity.getNickname());
        invite_num_tv.setText(userEntity.getInvitation_code()+"");
        GlideImageLoader.setAvatar(user_icon_iv, userEntity.getAvatar());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!isChangeAccount)
                    CertificationUtil.newInstance().authCerticicationCode(activity);
            }
        },3000);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        SheepApp.getInstance().setGameFlag(true);
    }
}
