package com.meeting.demo.dependency;

import android.app.Activity;
import android.graphics.Color;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import com.huawei.hwmconf.sdk.dependency.IUiCustomSubViewHandler;
import com.huawei.hwmconf.sdk.model.conf.entity.HWMCustomSubViewModel;
import com.huawei.hwmconf.sdk.model.conf.entity.HWMCustomSubViewPosition;
import com.huawei.hwmconf.sdk.util.Utils;
import com.huawei.hwmfoundation.utils.route.HCActivityManager;
import com.meeting.demo.view.sdk.CustomSubViewConfigFragment;
import com.meeting.demo.model.CustomSubViewModel;
import com.meeting.demo.utils.CustomSubViewCache;

public class CustomSubViewHandle implements IUiCustomSubViewHandler {
    @Nullable
    @Override
    public HWMCustomSubViewModel getMeetingCustomSubview(HWMCustomSubViewPosition position) {

        if (HWMCustomSubViewPosition.HWM_CUSTOM_SUBVIEW_POSITION_TOP_LEFT.equals(position)) {
            return getCustomSubViewModelTopLeft();
        } else if (HWMCustomSubViewPosition.HWM_CUSTOM_SUBVIEW_POSITION_BOTTOM_CENTER.equals(position)) {
            return getCustomSubViewModelBottomCenter();
        } else if (HWMCustomSubViewPosition.HWM_CUSTOM_SUBVIEW_POSITION_BOTTOM_RIGHT.equals(position)) {
            return getCustomSubViewModelBottomRight();
        }
        return null;
    }

    private HWMCustomSubViewModel getCustomSubViewModelTopLeft() {
        CustomSubViewModel customSubViewModel = CustomSubViewCache.getInstance().getCustomSubViewModelTopLeft();
        if (!customSubViewModel.isOpen()) {
            return null;
        }
        HWMCustomSubViewModel customSubViewModelTopLeft = new HWMCustomSubViewModel();
        TextView textView = new TextView(Utils.getResContext());
        textView.setText("TopLeft");
        textView.setTextColor(Color.WHITE);
        textView.setBackgroundColor(Color.RED);
        textView.setPadding(8, 36, 8, 36);
        textView.setOnClickListener(v -> showConfigActivity());
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        customSubViewModelTopLeft.setView(textView);
        customSubViewModelTopLeft.setShowFollowConfCtrlBar(customSubViewModel.isShowFollowConfCtrlBar());
        customSubViewModelTopLeft.setEnableAttach(customSubViewModel.isEnableAttach());
        customSubViewModelTopLeft.setEnableDrag(customSubViewModel.isEnableDrag());
        customSubViewModelTopLeft.setAttachType(customSubViewModel.getAttachType());
        return customSubViewModelTopLeft;
    }

    private HWMCustomSubViewModel getCustomSubViewModelBottomCenter() {
        CustomSubViewModel customSubViewModel = CustomSubViewCache.getInstance().getCustomSubViewModelBottomCenter();
        if (!customSubViewModel.isOpen()) {
            return null;
        }
        HWMCustomSubViewModel customSubViewModelBottomCenter = new HWMCustomSubViewModel();
        TextView textView = new TextView(Utils.getResContext());
        textView.setText("BottomCenter");
        textView.setTextColor(Color.WHITE);
        textView.setBackgroundColor(Color.BLUE);
        textView.setPadding(8, 36, 8, 36);
        textView.setOnClickListener(v -> showConfigActivity());
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        customSubViewModelBottomCenter.setView(textView);
        customSubViewModelBottomCenter.setShowFollowConfCtrlBar(customSubViewModel.isShowFollowConfCtrlBar());
        customSubViewModelBottomCenter.setEnableAttach(customSubViewModel.isEnableAttach());
        customSubViewModelBottomCenter.setEnableDrag(customSubViewModel.isEnableDrag());
        customSubViewModelBottomCenter.setAttachType(customSubViewModel.getAttachType());
        return customSubViewModelBottomCenter;
    }

    private HWMCustomSubViewModel getCustomSubViewModelBottomRight() {
        CustomSubViewModel customSubViewModel = CustomSubViewCache.getInstance().getCustomSubViewModelBottomRight();
        if (!customSubViewModel.isOpen()) {
            return null;
        }
        HWMCustomSubViewModel customSubViewModelBottomRight = new HWMCustomSubViewModel();
        TextView textView = new TextView(Utils.getResContext());
        textView.setText("BottomRight");
        textView.setTextColor(Color.WHITE);
        textView.setBackgroundColor(Color.GREEN);
        textView.setPadding(8, 36, 8, 36);
        textView.setOnClickListener(v -> showConfigActivity());
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        customSubViewModelBottomRight.setView(textView);
        customSubViewModelBottomRight.setShowFollowConfCtrlBar(customSubViewModel.isShowFollowConfCtrlBar());
        customSubViewModelBottomRight.setEnableAttach(customSubViewModel.isEnableAttach());
        customSubViewModelBottomRight.setEnableDrag(customSubViewModel.isEnableDrag());
        customSubViewModelBottomRight.setAttachType(customSubViewModel.getAttachType());
        return customSubViewModelBottomRight;
    }

    private void showConfigActivity() {
        Activity activity = HCActivityManager.getInstance().getCurrentActivity();
        if (activity instanceof FragmentActivity) {
            DialogFragment fragment = new CustomSubViewConfigFragment();
            fragment.show(((FragmentActivity) activity).getSupportFragmentManager(), null);
        }
    }
}
