package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.di.components.DaggerHomePageComponent;
import com.sheep.gamegroup.di.modules.HomePageModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.presenter.HomePageContract;
import com.sheep.gamegroup.presenter.HomePagePresenter;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.gamegroup.util.MyListview;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.G;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

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

public class HomePageAct extends BaseActivity implements HomePageContract.View{
    @BindView(R.id.icon_img_iv)
    ImageView iconImgIv;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.sheep_num_tv)
    TextView sheepNumTv;
    @BindView(R.id.user_layout)
    RelativeLayout userLayout;
    @BindView(R.id.withdrawal)
    TextView withdrawal;
    @BindView(R.id.my_price)
    TextView myPrice;
    @BindView(R.id.my_all_price)
    TextView myAllPrice;
    @BindView(R.id.try_play_layout)
    LinearLayout tryPlayLayout;
    @BindView(R.id.invitation_layout)
    LinearLayout invitationLayout;
    @BindView(R.id.lying_layout)
    LinearLayout lyingLayout;
    @BindView(R.id.listview)
    MyListview listview;

    @Inject
    HomePagePresenter pagePresenter;
    private Activity activity;
    private UserEntity userEntity;


    Handler mHanderl = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            myPrice.setText(userEntity.getBalance()+" 元");
            myAllPrice.setText("累计资产 "+userEntity.getTotal_asset()+" 元");
            nameTv.setText(userEntity.getNickname()+"");//绵羊号：123456789
            sheepNumTv.setText("绵羊号："+userEntity.getInvitation_code());
        }
    };
    @Override
    protected int getLayoutId() {
        return R.layout.homepage_act_layout;
    }

    @Override
    public void initView() {
        activity = this;
        DaggerHomePageComponent.builder()
                .netComponent(SheepApp.get(this).getNetComponent())
                .homePageModule(new HomePageModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        pagePresenter.getUserInfo();

    }

    @OnClick({R.id.icon_img_iv, R.id.user_layout, R.id.withdrawal, R.id.try_play_layout, R.id.invitation_layout, R.id.lying_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.icon_img_iv:
                break;
            case R.id.user_layout:
                Jump2View.getInstance().goPersonnalCenterView(activity, null);
                break;
            case R.id.withdrawal:
                Jump2View.getInstance().goWithdrawal(activity, null);
                break;
            case R.id.try_play_layout:
                Jump2View.getInstance().goTryplayView(activity, null);
                break;
            case R.id.invitation_layout:
                Jump2View.getInstance().goAskGetMoney(activity, null);
                break;
            case R.id.lying_layout:
                break;
        }
    }

    @Override
    public void updateData2View(Object o) {
        if(o == null){
            return;
        }
        BaseMessage baseMessage = (BaseMessage) o;
        userEntity = JSON.parseObject(JSONObject.toJSONString(baseMessage.getData()), UserEntity.class);
        try {
            MyDbManager.getInstance().saveOrUpdateUser(userEntity);
//            userEntity = MyDbManager.getInstance().dbFindUser();
            Log.e("------userEntity",userEntity.getNickname()+"|"+userEntity.getMobile()+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        mHanderl.sendEmptyMessage(0);

    }

    @Override
    public void failData2View(Object o) {
        G.showToast("网络错误");

    }
}
