package com.naz.sdkdemo.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.naz.sdkdemo.R;
import com.naz.sdkdemo.utils.MyUtils;

/**
 * @Date: 2022/4/9 5:53 下午
 * @author: sunjichang
 * @Description:
 */
public class TabRowView extends LinearLayout {
    private Callback callback = null;
    private CellClickCallback clickCallback;
    private int borderColor;
    private int count = 0;
    private LinearLayout layoutContainer;
    private View topBorder, bottomBorder, leftBorder, rightBorder;

    public TabRowView(Context context) {
        this(context, null);
    }

    public TabRowView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabRowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.VERTICAL);
        borderColor = getResources().getColor(R.color.grey_99);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, MyUtils.dp2px(context, 1));
        topBorder = new View(context);
        topBorder.setBackgroundColor(borderColor);
        topBorder.setVisibility(View.GONE);
        addView(topBorder, lp);

        FrameLayout layout = new FrameLayout(context);
        lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
        lp.weight = 1;
        addView(layout, lp);

        layoutContainer = new LinearLayout(context);
        layoutContainer.setGravity(Gravity.CENTER_VERTICAL);
        layoutContainer.setOrientation(LinearLayout.HORIZONTAL);
        FrameLayout.LayoutParams contentPrams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        layout.addView(layoutContainer, contentPrams);

        leftBorder = new View(context);
        leftBorder.setBackgroundColor(borderColor);
        leftBorder.setVisibility(View.GONE);
        FrameLayout.LayoutParams leftParams = new FrameLayout.LayoutParams(MyUtils.dp2px(context, 1),
                ViewGroup.LayoutParams.MATCH_PARENT);
        leftParams.gravity = Gravity.LEFT;
        layout.addView(leftBorder, leftParams);

        rightBorder = new View(context);
        rightBorder.setBackgroundColor(borderColor);
        rightBorder.setVisibility(View.GONE);
        FrameLayout.LayoutParams rightParams = new FrameLayout.LayoutParams(MyUtils.dp2px(context, 1),
                ViewGroup.LayoutParams.MATCH_PARENT);
        rightParams.gravity = Gravity.RIGHT;
        layout.addView(rightBorder, rightParams);

        bottomBorder = new View(context);
        bottomBorder.setBackgroundColor(borderColor);
        bottomBorder.setVisibility(View.GONE);
        lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, MyUtils.dp2px(context, 1));
        addView(bottomBorder, lp);
    }

    public void setCellCount(int count) {
        if (count == 0) {
            return;
        }
        this.count = count;
        buildViews();
    }

    private void buildViews() {
        layoutContainer.removeAllViews();
        if (null == callback) {
            return;
        }
        Cell cell = null;
        View child = null;
        LayoutParams lp = null;
        View splitView;
        LayoutParams tmpLp = null;
        float weight = 0;
        for (int i = 0; i < count; i++) {
            cell = callback.onCreateCell(i);
            if (null == cell || null == cell.view) {
                child = new View(getContext());
            } else {
                child = cell.view;
                weight = cell.weight;
                tmpLp = cell.params;
            }
            child.setTag(i);
            final int index = i;
            child.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    cellClick(v, index);
                }
            });
            if (tmpLp != null) {
                lp = tmpLp;
            } else {
                lp = new LayoutParams(weight > 0 ? 0 : ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                if (weight > 0) {
                    lp.weight = weight;
                }
                lp.gravity = Gravity.CENTER;
            }
            layoutContainer.addView(child, lp);
            if (i < count - 1) {
                splitView = new View(getContext());
                splitView.setBackgroundColor(borderColor);
                lp = new LayoutParams(MyUtils.dp2px(getContext(), 1), ViewGroup.LayoutParams.MATCH_PARENT);
                layoutContainer.addView(splitView, lp);
            }
        }
    }

    private void cellClick(View v, int index) {
        if (null != callback) {
            callback.onCellClick(v, index);
        }
        if (null != clickCallback) {
            clickCallback.onCellClick(v, index);
        }
    }

    public void setCellClickCallback(CellClickCallback callback) {
        this.clickCallback = callback;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void setBorderColor(int color) {
        borderColor = color;
        bottomBorder.setBackgroundColor(borderColor);
        topBorder.setBackgroundColor(borderColor);
        leftBorder.setBackgroundColor(borderColor);
        rightBorder.setBackgroundColor(borderColor);
    }

    public void setTopBorderVisible(boolean visible) {
        topBorder.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setBottomBorderVisible(boolean visible) {
        bottomBorder.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setLeftBorderVisible(boolean visible) {
        leftBorder.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setRightBorderVisible(boolean visible) {
        rightBorder.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setBorderVisible(boolean visible) {
        setTopBorderVisible(visible);
        setLeftBorderVisible(visible);
        setRightBorderVisible(visible);
        setBottomBorderVisible(visible);
    }

    public interface Callback {
        Cell onCreateCell(int index);
        void onCellClick(View cell, int index);
    }

    public interface CellClickCallback {
        void onCellClick(View cell, int index);
    }

    public <T extends View> T getCell(int index) {
        int finalIndex = index > 0 ? index * 2 : index;
        if (finalIndex < 0 || finalIndex > layoutContainer.getChildCount() -1) {
            return null;
        }
        try {
            View view = layoutContainer.getChildAt(finalIndex);
            T child = (T) view;
            return child;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public <T extends View> T findCellById(int resId) {
        if (resId <= 0) {
            return null;
        }
        try {
            T child = findViewById(resId);
            return child;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static class LayoutParams extends LinearLayout.LayoutParams {

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(int width, int height, float weight) {
            super(width, height, weight);
        }
    }

    public static class Cell {
        public View view;
        public float weight = 1;
        public LayoutParams params;
    }
}
