package BWP.MobileSale;

import org.forks.jsonrpc.JsonRpcException;
import org.forks.jsonrpc.RpcFacade;
import BWP.MobileSale.Approval.ApprovalListActivity;
import BWP.MobileSale.BranchOrder.BranchOrderMainActivity;
import BWP.MobileSale.BreedLog.BreedLogEditActivity;
import BWP.MobileSale.BreedLog.BreedLogListActivity;
import BWP.MobileSale.ChinaPay.ChinaPayMainActivity;
import BWP.MobileSale.MobileAdjustGoodsBill.MobileAdjustGoodsBillMainActivity;
import BWP.MobileSale.Order.OrderMainActivity;
import BWP.MobileSale.RemainGoods.RemainGoodsMainActivity;
import BWP.MobileSale.Return.ReturnMainActivity;
import BWP.MobileSale.SaleOutStore.SaleOutStoreMainActivity;
import BWP.MobileSale.Util.CallableTask;
import BWP.MobileSale.Util.DialogUtil;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends LoginedActivity {

	Button btnOrder = null;
	Button btnReturn = null;
	Button btnQuote = null;
	Button btnChangePwd = null;
	boolean enableSaleOrder = false;
	boolean enableMobileAdjustGoodsBill = false;
	boolean enableSaleXiWangEnable = false;	
	boolean enableReturn = false;
	boolean enableBranchOrder = false;
	boolean enableSaleOutStore = false;
	boolean enableChinaPay = false;	
	boolean enableUserMessage = true;
	@Override
    public void onCreate(Bundle savedInstanceState) {   	


    	
        super.onCreate(savedInstanceState);
        try {
			initRoles();
		} catch (JsonRpcException e) {
			e.printStackTrace();
		}
        initImageButton();
         
        setContentView(mainLayout());
     
    }

    private void initRoles() throws JsonRpcException
    {
    	String method="/Sale/Sale/Rpcs/MobileSaleClientRpc/OrderEnable";
    	try {
    		enableSaleOrder=RpcFacade.rpcCall(method);
		} catch (Exception e) {
		}
    	
    	 method="/Sale/Sale/Rpcs/MobileSaleClientRpc/MobileAdjustGoodsBillEnable";
    	try {
    		enableMobileAdjustGoodsBill = RpcFacade.rpcCall(method);
		} catch (Exception e) {
		}
    	
    	try {
    		method="/Sale/Sale/Rpcs/MobileSaleClientRpc/ReturnEnable";
        	enableReturn=RpcFacade.rpcCall(method);
		} catch (Exception e) {
		}
    	
    	try {
    		method="/Sale/Sale/Rpcs/MobileSaleClientRpc/BranchOrderEnable";
    		enableBranchOrder = RpcFacade.rpcCall(method);
		} catch (Exception e) {
			// TODO: handle exception
		}    	
    	try {
    		method="/Sale/Sale/Rpcs/MobileSaleClientRpc/SaleXiWangEnable";
        	enableSaleXiWangEnable = RpcFacade.rpcCall(method);
		} catch (Exception e) { 
		}
    	try {
    		method="/Sale/Sale/Rpcs/MobileSaleClientRpc/SaleOutStoreEnable";
    		enableSaleOutStore = RpcFacade.rpcCall(method);
		} catch (Exception e) { 
		}    	 	
    	try {
    		method="/Sale/SaleExportBase/ChinaPayRpc/ChinaPayEnable";
    		enableChinaPay = RpcFacade.rpcCall(method);
		} catch (Exception e) { 
		}    	
    }

    private EditText oldInput;
	private EditText newInput;
	private EditText newInputR;
	CallableTask changePasswordTask = new CallableTask(this, "修改密码中") {

		public Object call() throws Exception {
			
			if(!newInput.getText().toString().equals(newInputR.getText().toString())){
				throw new Exception("二次输入的新密码不同");
			}
			
			String method = "/MainSystem/MainSystem/Rpcs/UserRpc/ChangePassword";
			RpcFacade.rpcCall(method, oldInput.getText().toString(),newInput.getText().toString());		
			return null;
		}
		
		@Override
		protected void successUI() {
			DialogUtil.Alert(MainActivity.this, "您的密码修改成功");
			
		}
	};

	ImageView imageOrder = null;
	ImageView imageMobileAdjustGoodsBill = null;
	ImageView imageRemainGoods = null;
	ImageView imageReturn = null;
	ImageView imageRijie = null;
	ImageView imageApproval = null;
	ImageView imageBreedLog = null;
	ImageView imageChangepwd = null;
	ImageView imageUserMessage = null;
	ImageView imageSaleOutStore = null;
	ImageView imageChinaPay = null;	
	private void initImageButton()
	{
		imageOrder=new ImageView(MainActivity.this);
		//imageOrder.setImageResource(R.drawable.order);

        Drawable dr = this.getResources().getDrawable(R.drawable.order); //取得图片资源
		imageOrder.setBackgroundDrawable(dr);
		imageOrder.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, OrderMainActivity.class);
			}
		});
	
		imageSaleOutStore=new ImageView(MainActivity.this);
		imageSaleOutStore.setImageResource(R.drawable.outstore);
		imageSaleOutStore.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, SaleOutStoreMainActivity.class);
			}
		});
		
		imageChinaPay=new ImageView(MainActivity.this);
		imageChinaPay.setImageResource(R.drawable.pay);
		imageChinaPay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, ChinaPayMainActivity.class);
			}
		});		
		
		imageMobileAdjustGoodsBill=new ImageView(MainActivity.this);
		//imageOrder.setImageResource(R.drawable.order);

        Drawable dr2 = this.getResources().getDrawable(R.drawable.order); //取得图片资源
        imageMobileAdjustGoodsBill.setBackgroundDrawable(dr2);
        imageMobileAdjustGoodsBill.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, MobileAdjustGoodsBillMainActivity.class);
			}
		});
	 
		imageReturn=new ImageView(MainActivity.this);
		imageReturn.setImageResource(R.drawable.returnrecord);
		imageReturn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, ReturnMainActivity.class);
			}
		});
		
		imageRemainGoods=new ImageView(MainActivity.this);
		imageRemainGoods.setImageResource(R.drawable.remaingoods);
		imageRemainGoods.setOnClickListener(new OnClickListener() { 
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, RemainGoodsMainActivity.class);
			}
		});		
		
		imageRijie=new ImageView(MainActivity.this);
		imageRijie.setImageResource(R.drawable.rijie);
		imageRijie.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, BranchOrderMainActivity.class);				
			}
		});

	

		imageUserMessage = new ImageView(MainActivity.this);
		imageUserMessage.setImageResource(R.drawable.breedlog);
		imageUserMessage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, UserMessageActivity.class);				
			}
		});


 

		
		imageApproval=new ImageView(MainActivity.this);
		imageApproval.setImageResource(R.drawable.approval);
		imageApproval.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, ApprovalListActivity.class);
			}
		});
		
		
		imageBreedLog=new ImageView(MainActivity.this);
		imageBreedLog.setImageResource(R.drawable.breedlog);
		imageBreedLog.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.ActivityUtil.Go(MainActivity.this, BreedLogListActivity.class);
			}
		});
		
		
		
		imageChangepwd=new ImageView(MainActivity.this);
		imageChangepwd.setImageResource(R.drawable.changepwd);
		imageChangepwd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LinearLayout layout = new LinearLayout(MainActivity.this);
				layout.setOrientation(LinearLayout.VERTICAL);
				oldInput = new EditText(MainActivity.this);
				oldInput.setHint("旧的密码");
				oldInput.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
				newInput = new EditText(MainActivity.this);
				newInput.setHint("新的密码");
				newInput.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
				newInputR = new EditText(MainActivity.this);
				newInputR.setHint("确认新密码");
				newInputR.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
				layout.addView(oldInput);
				layout.addView(newInput);
				layout.addView(newInputR);
				new AlertDialog.Builder(MainActivity.this).setTitle("修改密码")
				.setView(layout)
				.setPositiveButton("修改",new DialogInterface.OnClickListener(){

					public void onClick(DialogInterface dialog, int which) {
						changePasswordTask.execute();
					}
				})
				.setNegativeButton("取消", null)
				.show();				
			}
		});
	}

	private LinearLayout mainLayout()
	{
		LinearLayout mayLayout = GetVLayoutWithBackgroud();
		AddImageButtons(mayLayout, new ImageViewIcon[]{
				new ImageViewIcon(imageOrder, "手机订单",enableSaleOrder),
				new ImageViewIcon(imageSaleOutStore, "销售出库",enableSaleOutStore),				
				new ImageViewIcon(imageReturn, "退货登记",enableReturn),
				new ImageViewIcon(imageRijie, "门店日结",enableBranchOrder),
				new ImageViewIcon(imageMobileAdjustGoodsBill, "手机调货单",enableMobileAdjustGoodsBill),
				new ImageViewIcon(imageRemainGoods, "门店留货单",enableSaleXiWangEnable),
				new ImageViewIcon(imageChinaPay, "收付捷支付",enableChinaPay),				
				new ImageViewIcon(imageUserMessage, "用户消息",enableUserMessage),
				new ImageViewIcon(imageChangepwd, "修改密码",true),
				
		});

		return mayLayout;
	}
	
	class ImageViewIcon{
		ImageView imageView;
		String imageViewName;
		Boolean Enable;
		public ImageViewIcon(ImageView image, String text, Boolean enable){
			imageView = image;
			imageViewName = text;
			Enable = enable;
		}
	}
	
	private void AddImageButtons(LinearLayout layout,ImageViewIcon[] imageViews){
		int count=0;
		int max=4;
		LinearLayout lineLayout = null;
		for(int i=0;i<imageViews.length;i++){
			if(!imageViews[i].Enable)
				continue;
			if(count==max)
				count=0;
			if(count==0){
				lineLayout=GetHLayout(max);
				
				TextView view1=new TextView(this);		
				layout.addView(view1);
				layout.addView(lineLayout);
			}
			lineLayout.addView(ImageAndTextView(imageViews[i].imageView, imageViews[i].imageViewName));
			count++;
		}
	}
	
	private LinearLayout GetHLayout(int maxCount){
		LinearLayout hLayout=new LinearLayout(MainActivity.this);
		hLayout.setOrientation(LinearLayout.HORIZONTAL);
		hLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		
		TextView view=new TextView(this);
		view.setText("zha");
		view.setVisibility(maxCount);
		hLayout.addView(view);
		return hLayout;
	}
	
	private LinearLayout GetVLayoutWithBackgroud(){
		LinearLayout vLayout=new LinearLayout(MainActivity.this);
		vLayout.setOrientation(LinearLayout.VERTICAL);
		vLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
		vLayout.setBackgroundResource(R.drawable.bg2);
		return vLayout;
	}
	
	private LinearLayout ImageAndTextView(ImageView image,String text)
	{
		LinearLayout layout=new LinearLayout(MainActivity.this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		layout.setLayoutParams(	new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		
		
		
		TextView view=new TextView(MainActivity.this);
		view.setText("  "+text);
		
		layout.addView(image);
		layout.addView(view);
		
		return layout;
	}
	
}
