package com.geek.demo.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.geek.demo.R;
import com.geek.demo.adapter.BannerAdapter;
import com.geek.demo.adapter.SceneServiceAdapter;
import com.geek.demo.model.SceneData;
import com.geek.demo.utils.MyUtils;

public class SceneCard extends LinearLayout {

    private Context context;
    private View llNoSceneContainer;
    private View llSceneContainer;
    private ImageView ivSceneLogo;
    private ImageView ivSceneLogo2;
    private TextView tvSceneName;

    private SceneData sceneData1;
    private SceneData sceneData2;
    private RecyclerView rvSceneService;
    private View bannerCard;
    private BannerAdapter bannerAdapter;
    private ViewPager bannerPager;
    private GridLayoutManager gridLayoutManager;
    private SceneServiceAdapter sceneServiceAdapter;

    public SceneCard(Context context) {
        super(context);
        init(context);
    }

    public SceneCard(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        initView();
    }

    private void initView() {
        View rootView = View.inflate(context, R.layout.scene_card, this);
        llNoSceneContainer = rootView.findViewById(R.id.ll_no_scene_container);
        llSceneContainer = rootView.findViewById(R.id.ll_scene_container);
        ivSceneLogo = rootView.findViewById(R.id.iv_scene_logo);
        ivSceneLogo2 = rootView.findViewById(R.id.iv_scene_logo2);
        tvSceneName = rootView.findViewById(R.id.tv_scene_name);
        rvSceneService = rootView.findViewById(R.id.rv_scene_service);
        bannerCard = rootView.findViewById(R.id.banner_card);
        bannerPager = rootView.findViewById(R.id.banner);
        ivSceneLogo2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sceneData2 != null && sceneData1 != null) {
                    SceneData temp = sceneData1;
                    sceneData1 = sceneData2;
                    sceneData2 = temp;
                    notifySceneCard(sceneData1, sceneData2);
                }
            }
        });
    }

    public void notifySceneCard(SceneData sceneData1, SceneData sceneData2) {
        this.sceneData1 = sceneData1;
        this.sceneData2 = sceneData2;

        if (sceneData1 == null && sceneData2 == null) {
            llNoSceneContainer.setVisibility(VISIBLE);
            llSceneContainer.setVisibility(GONE);
        } else {
            llNoSceneContainer.setVisibility(GONE);
            llSceneContainer.setVisibility(VISIBLE);
            bannerCard.setVisibility(GONE);
            rvSceneService.setVisibility(GONE);
            if (sceneData1 != null) {
                tvSceneName.setText(sceneData1.shopName);
                MyUtils.loadImage(sceneData1.logoUrl, ivSceneLogo, context);
                if (sceneData1.bannerList != null && !sceneData1.bannerList.isEmpty()) {
                    bannerCard.setVisibility(VISIBLE);
                    if (bannerAdapter == null) {
                        bannerAdapter = new BannerAdapter(context, sceneData1.bannerList);
                        bannerPager.setAdapter(bannerAdapter);
                    } else {
                        bannerAdapter.setData(sceneData1.bannerList);
                    }
                } else {
                    bannerCard.setVisibility(GONE);
                }

                if (sceneData1.service != null && !sceneData1.service.isEmpty()) {
                    rvSceneService.setVisibility(VISIBLE);
                    if (gridLayoutManager == null) {
                        gridLayoutManager = new GridLayoutManager(context, 4);
                        rvSceneService.setLayoutManager(gridLayoutManager);
                    }

                    if (sceneServiceAdapter == null) {
                        sceneServiceAdapter = new SceneServiceAdapter(rvSceneService);
                        rvSceneService.setAdapter(sceneServiceAdapter);
                    }
                    sceneServiceAdapter.setData(sceneData1.service);
                } else {
                    rvSceneService.setVisibility(GONE);
                }
            }
            if (sceneData2 != null) {
                ivSceneLogo2.setVisibility(VISIBLE);
                MyUtils.loadImage(sceneData2.logoUrl, ivSceneLogo2, context);
            } else {
                ivSceneLogo2.setVisibility(GONE);
            }
        }
    }
}
