package com.android.chmo.ui.fragment;

import android.app.Dialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.app.ChmoApplication;
import com.android.chmo.base.BaseFragment;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.LoginRes;
import com.android.chmo.http.service.UserService;
import com.android.chmo.im.IMManager;
import com.android.chmo.model.LoginUser;
import com.android.chmo.ui.activity.MyWalletActivity;
import com.android.chmo.ui.activity.me.AboutActivity;
import com.android.chmo.ui.activity.me.RechargeActivity;
import com.android.chmo.ui.activity.me.SettingsActivity;
import com.android.chmo.ui.activity.order.MyOrderActivity;
import com.android.chmo.ui.activity.order.VideoOrderActivity;
import com.android.chmo.ui.activity.promote.PromoteActivity;
import com.android.chmo.ui.dialog.ConfirmDialog;
import com.android.chmo.utils.LoginUtils;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/31.
 */

public class MineFragment extends BaseFragment {
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView  tvName;

    @Override
    public int getLayoutRes() {
        return R.layout.page_mine;
    }

    @Override
    public void initView() {
        if (ChmoApplication.isLogin()) {
            LoginUser user = LoginUtils.getLoginUser();
            tvName.setText(user.getPet());
            XUtilsImage.displayCircluar(ivAvatar, HttpApi.getImgUrl(user.getHphoto()), R.mipmap.def_head);
        } else {
            tvName.setText("未登录");
            XUtilsImage.displayCircluar(ivAvatar, "", R.mipmap.def_head);
        }
    }

    @Override
    public void onVisible() {
        getUserInfo();
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    private void getUserInfo() {
        if (ChmoApplication.isLogin()) {
            UserService.getUser(new RequestCallback() {
                @Override
                public void onSuccess(String result) {
                    LoginRes res = new Gson().fromJson(result, LoginRes.class);
                    if (res.data != null) {
                        LoginUser user = new LoginUser();
                        user.setUser(res.data);
                        LoginUtils.saveLoginUser(user);
                        IMManager.updateUser();
                        initView();
                    }
                }

                @Override
                public void onFailure(String error) {
                }
            });
        }
    }

    @OnClick({R.id.ll_order, R.id.btn_promote, R.id.ll_jump,
            R.id.ll_wallet, R.id.ll_video_order, R.id.ll_about,
            R.id.iv_settings, R.id.iv_avatar})
    void onClick(View v) {
        if (v.getId() == R.id.ll_about) {
            openPage(AboutActivity.class);
        } else {
            if (!ChmoApplication.isLogin()) {
                ChmoApplication.showLoginDialog(this.getActivity());
                return;
            }
        }
        switch (v.getId()) {
            case R.id.ll_order:
                openPage(MyOrderActivity.class);
                break;
            case R.id.ll_wallet:
                openPage(MyWalletActivity.class);
                break;
            case R.id.ll_video_order:
                openPage(VideoOrderActivity.class);
                break;
            case R.id.btn_promote:
                openPage(PromoteActivity.class);
                break;
            // case R.id.ll_about:
            //     openPage(AboutActivity.class);
            //     break;
            case R.id.iv_settings:
            case R.id.iv_avatar:
                openPage(SettingsActivity.class);
                break;
            case R.id.ll_jump:
                new ConfirmDialog(this.getActivity())
                        .setButton("确定", "取消")
                        .setMsg("模特版仅对iOS用户开放，请在AppStore中搜索\"千模模特版\"")
                        .setDialogListener(new ConfirmDialog.DialogListener() {
                            @Override
                            public void onOk(Dialog dialog) {
                                dialog.dismiss();
                            }

                            @Override
                            public void onCancel(Dialog dialog) {
                                dialog.dismiss();
                            }
                        }).show();
                break;
        }
    }


}
