package com.sheep.gamegroup.view.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseUMActivity;
import com.sheep.gamegroup.di.components.DaggerLoginComponent;
import com.sheep.gamegroup.di.modules.LoginModule;
import com.sheep.gamegroup.presenter.LoginContract;
import com.sheep.gamegroup.presenter.LoginPresenter;
import com.sheep.gamegroup.util.ChannelContent;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.SpUtils;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljy on 2018/3/8.
 */

public class LoginAct extends BaseUMActivity implements LoginContract.View {

    @Inject
    LoginPresenter mPresenter;
    @BindView(R.id.tel_login_tv)
    TextView telLoginTv;
    @BindView(R.id.iv_wx)
    ImageView ivWx;
    @BindView(R.id.iv_qq)
    ImageView ivQq;


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

    @Override
    public void initView() {
        DaggerLoginComponent.builder()
                .netComponent(SheepApp.get(this).getNetComponent())
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
        TestUtil.testChange(this, (TextView) findViewById(R.id.test_change));

    }


    private void requestPremiss() {
//        if (Build.VERSION.SDK_INT >= 23) {
//            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
//            ActivityCompat.requestPermissions(this, mPermissionList, 123);
//        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {


    }


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        requestPremiss();
        ChannelContent.getInstance().initChannelContent(this);


    }




    private void goTelLogin() {
        String openId = SpUtils.getOpenId(getApplicationContext());
        if (!TextUtils.isEmpty(openId)) {
            Jump2View.getInstance().goHomePageView(this, null);
            finish();
        } else {
            Jump2View.getInstance().goPhoneLoginView(LoginAct.this, 0);
//            finish();
        }
    }

    private void goWeixin() {
        UMShareAPI.get(this).doOauthVerify(this, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA platform) {
                showProgress();
            }

            @Override
            public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
                mPresenter.logByAuther(1,data.get("access_token"),data.get("openid"));
            }

            @Override
            public void onError(SHARE_MEDIA platform, int action, Throwable t) {
                hideProgress();
                String  str=t.getMessage();
                if(str.indexOf("2008")>0) {
                    showToast("微信授权失败,请先安装微信客户端 在登录吧！");

                }else{
                    showToast("微信授权失败  请检查网络是否正常");

                }

            }

            @Override
            public void onCancel(SHARE_MEDIA platform, int action) {
                showToast("微信授权取消");
                hideProgress();
            }
        });

    }


    private void loginQQ() {
        UMShareAPI.get(this).doOauthVerify(this, SHARE_MEDIA.QQ, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA platform) {
                 showProgress();
            }

            @Override
            public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//                mPresenter.logByAuther(2,data.get("openid"));
                mPresenter.logByAuther(2,data.get("access_token"),data.get("openid"));

            }

            @Override
            public void onError(SHARE_MEDIA platform, int action, Throwable t) {
                showToast("QQ授权失败  请检查网络是否正常");
                hideProgress();
            }

            @Override
            public void onCancel(SHARE_MEDIA platform, int action) {

                hideProgress();
            }
        });


    }


    @Override
    public void NetSuccess(int code, String msg) {
        showToast(msg);
        hideProgress();
        Jump2View.getInstance().goHomePageView(this, null);
        finish();

    }

    @Override
    public void NetError(int code, String errorMsg) {
        showToast(errorMsg);
        hideProgress();

    }



    @OnClick({R.id.tel_login_tv, R.id.iv_wx, R.id.iv_qq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tel_login_tv:
                goTelLogin();
                break;
            case R.id.iv_wx:
                goWeixin();
                break;
            case R.id.iv_qq:
                loginQQ();
                break;
        }
    }
}
