package com.sheep.gamegroup.module.login.fragments;


import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.LoginEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.module.login.controller.LoginController;
import com.sheep.gamegroup.util.ChannelContent;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.FastJsonUtils;
import com.sheep.gamegroup.util.PreferenceUtils;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.G;
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 butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.LOGIN_QQ;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.LOGIN_WX;

/**
 * A simple {@link Fragment} subclass.
 */
public class AbsLoginFgt extends BaseFragment {

    protected LoginController mController;

    public AbsLoginFgt() {
        // Required empty public constructor
    }

    public static AbsLoginFgt newInstance(LoginController controller) {
        AbsLoginFgt fragment = new AbsLoginFgt();
        fragment.mController = controller;
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void onViewCreated() {

    }

    @OnClick(R.id.wx_login_btn)
    public void doWxLogin(View v) {
        LOGIN_WX.onEvent();
        UMShareAPI.get(SheepApp.getInstance()).doOauthVerify(getActivity(), SHARE_MEDIA.WEIXIN, new ThirdpartAuthListener("微信") {
            @Override
            public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
                JSONObject j = new JSONObject();
                j.put("code", data.get("access_token"));
                j.put("username", data.get("openid"));
                j.put("scope", ChannelContent.getInstance().getChannel_name());
                PreferenceUtils.setPrefString(SheepApp.getInstance(), UMConfigUtils.LOGIN_TYPE, UMConfigUtils.Source.WECHAT);
                SheepApp.getInstance().getNetComponent().getApiService()
                        .LoginByWX(j)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                            @Override
                            public void onError(BaseMessage baseMessage) {
                                G.shortToast("微信登录失败  请检查网络是否正常");
                                hideProgress();
                            }

                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                hideProgress();
                                if (baseMessage != null) {
                                    LoginEntity loginEty = FastJsonUtils.toBean(JSONObject.toJSONString(baseMessage.getData()), LoginEntity.class);
                                    SpUtils.saveToken(SheepApp.getInstance(), loginEty.getToken());
                                    DataUtil.getInstance().initUserEntity(loginEty.getUser());
                                    mController.whenLoginSuccess(LoginController.PLATFORM_WX, loginEty);
                                    TestUtil.saveUser(loginEty);
                                }
                            }
                        });

            }
        });
    }

    @OnClick(R.id.qq_login_btn)
    public void doQQLogin(View v) {
        LOGIN_QQ.onEvent();
        UMShareAPI.get(SheepApp.getInstance()).doOauthVerify(getActivity(), SHARE_MEDIA.QQ, new ThirdpartAuthListener("QQ") {
            public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
                JSONObject j = new JSONObject();
                j.put("code", data.get("access_token"));
                j.put("username", data.get("openid"));
                j.put("scope", ChannelContent.getInstance().getChannel_name());
                PreferenceUtils.setPrefString(SheepApp.getInstance(), UMConfigUtils.LOGIN_TYPE, UMConfigUtils.Source.QQ);
                SheepApp.getInstance().getNetComponent().getApiService()
                        .LoginByQQ(j)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                G.shortToast("QQ登录失败  请检查网络是否正常");
                                hideProgress();
                            }

                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                hideProgress();
                                if (baseMessage != null) {
                                    LoginEntity loginEty = FastJsonUtils.toBean(JSONObject.toJSONString(baseMessage.getData()), LoginEntity.class);
                                    SpUtils.saveToken(SheepApp.getInstance(), loginEty.getToken());
                                    DataUtil.getInstance().initUserEntity(loginEty.getUser());
                                    mController.whenLoginSuccess(LoginController.PLATFORM_QQ, loginEty);
                                    TestUtil.saveUser(loginEty);
                                }
                            }
                        });
            }
        });
    }

    protected abstract class ThirdpartAuthListener implements UMAuthListener {

        private String platformName;

        public ThirdpartAuthListener(String platformName) {
            this.platformName = platformName;
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {
            showProgress();
        }

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

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

}
