package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.kfzs.duanduan.fragment.FgtPersonalCenter;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerPersonalCenterComponent;
import com.sheep.gamegroup.di.modules.PersonalCenterModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.presenter.PersonalCenterContract;
import com.sheep.gamegroup.presenter.PersonalCenterPresenter;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import javax.inject.Inject;

/**
 * 个人中心首页
 * Created by ljy on 2018/3/10.
 */

public class PersonalCenterAct extends BaseActivity implements PersonalCenterContract.View{

    Activity activity;
    @Inject
    PersonalCenterPresenter presenter;
    UserEntity userEntity;

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

    @Override
    public void initView() {
        activity = this;
        DaggerPersonalCenterComponent.builder()
                .netComponent(SheepApp.get(this).getNetComponent())
                .personalCenterModule(new PersonalCenterModule(this))
                .build()
                .inject(this);

        TitleBarUtils
                .getInstance()
                .setTitle(this, "个人中心")
                .setTitleColor(this, getResources().getColor(R.color.white))
                .setTitleListen(this, R.drawable.narrow_back_white, null)
                .setTitleBackgroud(this, R.drawable.sp_bg_gradient_blue_nocorners)
                .setShowOrHideLine(this, false)
                .setTitleFinish(this);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Bundle bundle = new Bundle();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("tag");
        if(fragment == null){
            fragment = new FgtPersonalCenter();
            fragment.setArguments(bundle);
            transaction.add(R.id.frame_container, fragment, "tag");
            transaction.commit();
        }else {
            fragment.setArguments(bundle);
            transaction.replace(R.id.frame_container, fragment);
            transaction.commit();
        }


    }

    @Override
    public void initListener() {

    }

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

    }

    @Override
    public void updateData2View(Object o) {
        Log.e("---getInfo-", o.toString());
        if(o == null){
            return;
        }
        BaseMessage baseMessage = (BaseMessage) o;
        userEntity = JSON.parseObject(JSONObject.toJSONString(baseMessage.getData()), UserEntity.class);

        try {
            MyDbManager.getInstance().saveOrUpdateUser(userEntity);
            UserEntity userEntitys = MyDbManager.getInstance().dbFindUser();
            Log.e("------userEntity",userEntity.getNickname()+"|"+userEntity.getMobile()+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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