package com.yutou.jianr_mg.Fragments;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Adapters.MyItemGridViewAdapter;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Data.UUserdata;
import com.yutou.jianr_mg.Data.User;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.views.UserDataActivity;


import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;


/**
 * Created by 58381 on 2018/2/22.
 */

public class My extends Fragment {
    private ImageLoader imageLoader;
    private View view;
    private User user;
    private UUserdata udata;
    private TextView uname,item_title,credit;
    private ImageView icon,item_icon,image_top;
     private LinearLayout items;
    private ListView myItems;

    private static My my;
    public static My init() {
        if(my==null){
            my=new My();
        }
        return my;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view==null){
            view=LayoutInflater.from(getContext()).inflate(R.layout.fragment_my,null);
            uname=view.findViewById(R.id.uname);
            icon=view.findViewById(R.id.icon);
            item_title=view.findViewById(R.id.item_title);
            item_icon=view.findViewById(R.id.item_icon);
            image_top=view.findViewById(R.id.image_top);
             items=view.findViewById(R.id.items);
            myItems=view.findViewById(R.id._my_items);
            credit=view.findViewById(R.id.credit);
        }
        icon.setOnClickListener(view->{
            startActivity(new Intent(getContext(), UserDataActivity.class));
        });
        image_top.setOnClickListener(view->Utils.toast(getContext(),"点这里不会有什么奇怪的东西啦"));
        imageLoader= Utils.initImageLoader(getContext());
        initData();
        return view;
    }
    private void initData(){
        user= AppData.user;
        udata=AppData.userdata;
        boolean isImage=true;
        if(udata==null) {
            udata = new UUserdata();
        }
        if(user==null||user.getId()<1){
            uname.setText("未登录");
            icon.setImageResource(R.mipmap.icon_logo);
            return;
        }
        if(udata.getImage()==null||udata.getImage().equals("#")){
            icon.setImageResource(R.mipmap.icon_logo);
            isImage=false;
        }
        try {
            uname.setText(user.getUname());
            if(isImage) {
                imageLoader.displayImage(udata.getImage(), icon);
                Glide.with(getContext()).load(udata.getImage()).apply(bitmapTransform(new BlurTransformation(25))).into(image_top);
            }else{
                Glide.with(getContext()).load(R.mipmap.icon_logo).apply(bitmapTransform(new BlurTransformation(25))).into(image_top);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        credit.setText("积分:"+udata.getCredit());
        myItems.setAdapter(new MyItemGridViewAdapter(getItemData(),getContext()));
    }
    private List<MyItemGridViewAdapter.ItemData> getItemData(){
        List<MyItemGridViewAdapter.ItemData> list=new ArrayList<>();
        list.add(new MyItemGridViewAdapter.ItemData(R.drawable.ic_download,"下载中心"));
        list.add(new MyItemGridViewAdapter.ItemData(R.drawable.ic_collection,"收藏夹"));
        list.add(new MyItemGridViewAdapter.ItemData(R.drawable.ic_feedback,"问题/建议"));
        list.add(new MyItemGridViewAdapter.ItemData(R.drawable.ic_config,"设置"));
      /*  list.add(new MyItemGridViewAdapter.ItemData(-1,""));
        list.add(new MyItemGridViewAdapter.ItemData(-1,""));
        list.add(new MyItemGridViewAdapter.ItemData(-1,""));
        list.add(new MyItemGridViewAdapter.ItemData(-1,""));*/
        return list;
    }


}
