package com.example.orangesale_end.fragment;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.orangesale_end.R;
import com.example.orangesale_end.adapter.ProductAdapter;
import com.example.orangesale_end.entity.OrangeProductPack;
import com.example.orangesale_end.entity.Product;
import com.example.orangesale_end.netrequest.OkHttpClientProduct;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


public class IndexFragment extends Fragment implements View.OnClickListener {
    private SearchView searchView;
    private LinearLayout orangeLine, youziLine, juziLine, xiguaLine, liLine, appleLine, lemonLine, mangguoLine;
    private GridView gridView;
    private ProductAdapter productAdapter;
    private List<Product> productList;
    private List<OrangeProductPack> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.content_index, container, false);
        init(view);
        return view;
    }

    /**
     * 初始化组件
     */
    private void init(View view) {
        searchView = view.findViewById(R.id.searchView);
        searchView.setOnClickListener(this);
        orangeLine = view.findViewById(R.id.qunzhuang);
        orangeLine.setOnClickListener(this);
        youziLine = view.findViewById(R.id.kuzhuang);
        youziLine.setOnClickListener(this);
        juziLine = view.findViewById(R.id.shangzhuang);
        juziLine.setOnClickListener(this);
        xiguaLine = view.findViewById(R.id.taozhuang);
        xiguaLine.setOnClickListener(this);
        liLine = view.findViewById(R.id.waitao);
        liLine.setOnClickListener(this);
        lemonLine = view.findViewById(R.id.niuzaiku);
        lemonLine.setOnClickListener(this);
        mangguoLine = view.findViewById(R.id.lianyiqun);
        mangguoLine.setOnClickListener(this);
        appleLine = view.findViewById(R.id.weiyi);
        appleLine.setOnClickListener(this);
        gridView = view.findViewById(R.id.index_famous_gridview);
        //initData();
        new SearchProductTask().execute();
    }


    @Override
    public void onClick(View v) {

    }

    /**
     * 初始化商品数据
     */
    private void initData() {
        productList = new ArrayList<>();
        Product product = new Product();
        product.setImageUrlId(R.drawable.qunzhuang);
        product.setProductName("裙装");
        product.setProductPrice(new BigDecimal("9.9"));
        Product product1 = new Product();
        product1.setImageUrlId(R.drawable.kuzhuang);
        product1.setProductName("裤装");
        product1.setProductPrice(new BigDecimal("29.9"));
        Product product2 = new Product();
        product2.setImageUrlId(R.drawable.shuangzhuang);
        product2.setProductName("上装");
        product2.setProductPrice(new BigDecimal("19.9"));
        Product product3 = new Product();
        product3.setImageUrlId(R.drawable.touzhuang);
        product3.setProductName("套装");
        product3.setProductPrice(new BigDecimal("19.9"));
        Product product4 = new Product();
        product4.setImageUrlId(R.drawable.waitao);
        product4.setProductName("外套");
        product4.setProductPrice(new BigDecimal("49.9"));
        Product product5 = new Product();
        product5.setImageUrlId(R.drawable.niuzaiku);
        product5.setProductName("牛仔裤");
        product5.setProductPrice(new BigDecimal("9.9"));
        productList.add(product);
        productList.add(product1);
        productList.add(product2);
        productList.add(product3);
        productList.add(product4);
        productList.add(product5);

    }

    /**
     * 发送网络请求获取数据
     */
    class SearchProductTask extends AsyncTask<Void, Void, List<OrangeProductPack>> {

        @Override
        protected List<OrangeProductPack> doInBackground(Void... voids) {
            OkHttpClientProduct clientProduct = new OkHttpClientProduct();
            try {
                list = clientProduct.getProductPack();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return list;
        }

        @Override
        protected void onPostExecute(List<OrangeProductPack> orangeProducts) {
            productAdapter = new ProductAdapter(getActivity(), list);
            gridView.setAdapter(productAdapter);
        }
    }
}
