package com.sheep.gamegroup.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sheep.gamegroup.absBase.AbsChooseImageActivity;
import com.sheep.gamegroup.event.ImageHeadChange;
import com.sheep.gamegroup.event.UserNameChange;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.util.AppUtil;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2018/3/16.
 */

public class PersionInfoAct extends AbsChooseImageActivity {

    UserEntity mEntity;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_nike)
    TextView tvNike;
    @BindView(R.id.tv_sheep_id)
    TextView tvSheepId;
    @BindView(R.id.tv_qr)
    ImageView tvQr;
    @BindView(R.id.tv_qr_small)
    ImageView tv_qr_small;
    @BindView(R.id.rl_qr)
    RelativeLayout rlQr;

    @BindView(R.id.rl_head)
    RelativeLayout rl_head;
    @BindView(R.id.iv_head_right)
    ImageView ivHeadRight;
    @BindView(R.id.layout_more1)
    RelativeLayout layout_more1;


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

    @Override
    public void initView() {
        ButterKnife.bind(this);
        TitleBarUtils
                .getInstance()
                .setTitle(this, "个人资料")
                .setTitleFinish(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        Intent i = getIntent();
        if (i != null && i.hasExtra("info")) {
            mEntity = (UserEntity) i.getSerializableExtra("info");
            if (mEntity != null) {
                tvNike.setText(mEntity.getNickname());
                tvSheepId.setText(mEntity.getInvitation_code());

                Glide.with(PersionInfoAct.this)
                        .load(AppUtil.getQRLink(mEntity.getShare_link(), 90))
                        .into(tvQr);
                Glide.with(PersionInfoAct.this)
                        .load("http://cdngame.kuaifazs.com/icon.png")
                        .apply(new RequestOptions().override(23))
                        .into(tv_qr_small);

                Glide.with(PersionInfoAct.this)
                        .load(mEntity.getAvatar())
                        .apply(new RequestOptions().placeholder(R.drawable.icon))
                        .into(ivHeadRight);
            }
        }

        EventBus.getDefault().register(this);

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UserNameChange done) {
        UserEntity user = mEntity;
        user.setNickname(done.getNikeName());
        mEntity = user;
        tvNike.setText(mEntity.getNickname());

    }


    @OnClick({R.id.layout_more1, R.id.rl_qr, R.id.rl_head})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_more1:
                Intent i = new Intent(PersionInfoAct.this, ReNameActivity.class);
                i.putExtra("name", mEntity.getNickname());
                startActivity(i);
                break;
            case R.id.rl_qr:
                ViewUtil.showQR(PersionInfoAct.this, mEntity.getShare_link());
                break;
            case R.id.rl_head:
                showChooseDialog();
                break;
        }
    }

    @Override
    protected void onNotGetImage(String msg) {
        G.showToast(msg);
    }

    @Override
    protected void onGetImage(String path) {
        GlideImageLoader.displayImage(this, ivHeadRight, path, R.drawable.icon);
    }

    @Override
    protected void onSetNetImageUrl(final String url) {
        setLoaddingText("更新中");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("avatar", url);
        SheepApp.get(this).getNetComponent().getApiService().changeBaseInfo(jsonObject)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseMessage>() {
                    @Override
                    public void call(BaseMessage baseMessage) {
                        setLoaddingText("完成");
                        onGetNetImageUrl(url);
                        dismissLoaddingDialog();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        updateError("失败");
                    }
                });
    }

    @Override
    protected void onGetNetImageUrl(String url) {
        ImageHeadChange imageHeadChange = new ImageHeadChange();
        imageHeadChange.setAvatar(url);
        EventBus.getDefault().post(imageHeadChange);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
