package com.navinfo.outdoor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.activity.FragmentManagement;
import com.navinfo.outdoor.activity.UserActivity;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.GetPriceBean;
import com.navinfo.outdoor.bean.MeasureBean;
import com.navinfo.outdoor.http.Callback;
import com.navinfo.outdoor.http.HttpInterface;
import com.navinfo.outdoor.http.OkGoBuilder;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;

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

/**
 * 我的Fragment
 * 2021-5-25
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    private ImageView imageHeard, imageShare, imageSign;
    private TextView tvName, tvMoney;
    private Button btnWithdraw, btnQuit;
    private RelativeLayout rlGrade, rlPrivilege, rlMap, rlIssue, rlContact, rlSet, rlAbout;

    public static MineFragment newInstance(Bundle bundle) {
        MineFragment fragment = new MineFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }

    }
    @Override
    protected int getLayout() {
        return R.layout.mine_fragment;
    }

    @Override
    protected void initView() {
        super.initView();
        imageHeard = findViewById(R.id.image_heard);
        Glide.with(getActivity()).load(R.drawable.mine_harder).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(imageHeard);
        imageHeard.setOnClickListener(this::onClick);
        tvName = findViewById(R.id.tv_name);
        tvName.setText(Constant.USER_NAME);
        tvName.setOnClickListener(this::onClick);
        tvMoney = findViewById(R.id.tv_money);
        imageShare = findViewById(R.id.image_share);
        imageShare.setOnClickListener(this::onClick);
        imageSign = findViewById(R.id.image_sign);
        imageSign.setOnClickListener(this::onClick);
        btnWithdraw = findViewById(R.id.btn_withdraw);
        btnWithdraw.setOnClickListener(this::onClick);
        rlGrade = findViewById(R.id.rl_grade);
        rlGrade.setOnClickListener(this::onClick);
        rlPrivilege = findViewById(R.id.rl_privilege);
        rlPrivilege.setOnClickListener(this::onClick);
        rlMap = findViewById(R.id.rl_map);
        rlMap.setOnClickListener(this::onClick);
        rlIssue = findViewById(R.id.rl_issue);
        rlIssue.setOnClickListener(this::onClick);
        rlContact = findViewById(R.id.rl_contact);
        rlContact.setOnClickListener(this::onClick);
        rlSet = findViewById(R.id.rl_set);
        rlSet.setOnClickListener(this::onClick);
        rlAbout = findViewById(R.id.rl_about);
        rlAbout.setOnClickListener(this::onClick);
        btnQuit = findViewById(R.id.btn_quit);
        btnQuit.setOnClickListener(this::onClick);
        initNetWork();
    }
    @Subscribe
    public void onEvent(Message data) {
        if (data.what == Constant.HOME_MINE) {
            if ((boolean)data.obj){
                initNetWork();
            }
        }
    }

    private void initNetWork() {
        showLoadingDialog();
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.GET_PRICE)
                .cls(GetPriceBean.class)
                .params(new HttpParams())
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<GetPriceBean>() {
                    @Override
                    public void onSuccess(GetPriceBean response, int id) {
                        dismissLoadingDialog();
                        if (response.getCode() == 200){
                            GetPriceBean.BodyBean body = response.getBody();
                            if (body!=null){
                                Double userPrice =body.getUserPrice();
                                tvMoney.setText(userPrice + "");
                            }
                        }else {
                            Toast.makeText(getActivity(), response.getMessage()+"", Toast.LENGTH_SHORT).show();
                        }


                        Log.d("TAG", "onSuccess: " + response.toString() + "tttttttt");
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Log.d("TAG", "onError: " + e.getMessage());
                    }
                });
        dismissDialog();
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_heard:
            case R.id.tv_name://跳转到个人信息
                Intent intentUser = new Intent(getActivity(), UserActivity.class);
                intentUser.putExtra("tag", 16);
                startActivity(intentUser);
                break;
            case R.id.image_share://分享
                Toast.makeText(getActivity(), "分享", Toast.LENGTH_SHORT).show();
                break;
            case R.id.image_sign://签到
                Toast.makeText(getActivity(), "签到", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_withdraw://提现页面

                Intent intentWithdraw = new Intent(getActivity(), FragmentManagement.class);
                intentWithdraw.putExtra("tag", 15);
                startActivity(intentWithdraw);
                break;
            case R.id.rl_grade://我的等级
                Intent intentGrade = new Intent(getActivity(), FragmentManagement.class);
                intentGrade.putExtra("tag", 17);
                startActivity(intentGrade);
                break;
            case R.id.rl_privilege://我的特权
                Intent intentPrivilege = new Intent(getActivity(), FragmentManagement.class);
                intentPrivilege.putExtra("tag", 18);
                startActivity(intentPrivilege);
                break;
            case R.id.rl_map://地图下载
                Intent intentMap = new Intent(getActivity(), FragmentManagement.class);
                intentMap.putExtra("tag", 19);
                startActivity(intentMap);
                break;
            case R.id.rl_issue://常见问题
                Intent intentIssue = new Intent(getActivity(), FragmentManagement.class);
                intentIssue.putExtra("tag", 20);
                startActivity(intentIssue);
                break;
            case R.id.rl_contact://联系我们
                Intent intentContact = new Intent(getActivity(), FragmentManagement.class);
                intentContact.putExtra("tag", 21);
                startActivity(intentContact);
                break;
            case R.id.rl_set://设置
                Intent intentSet = new Intent(getActivity(), FragmentManagement.class);
                intentSet.putExtra("tag", 22);
                startActivity(intentSet);
                break;
            case R.id.rl_about://关于
                Intent intentAbout = new Intent(getActivity(), FragmentManagement.class);
                intentAbout.putExtra("tag", 23);
                startActivity(intentAbout);
                break;
            case R.id.btn_quit://退出登录
                getActivity().finish();
                break;
        }
    }
    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this))//加上判断
            EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

}
